{"id":22517,"date":"2021-12-17T08:46:17","date_gmt":"2021-12-16T22:46:17","guid":{"rendered":"https:\/\/database.guide\/?p=22517"},"modified":"2023-01-24T18:23:58","modified_gmt":"2023-01-24T08:23:58","slug":"convert-in-sql-server","status":"publish","type":"post","link":"https:\/\/database.guide\/convert-in-sql-server\/","title":{"rendered":"CONVERT() in SQL Server"},"content":{"rendered":"\n<p>In SQL Server, the <code>CONVERT()<\/code> function converts an expression of one data type to another.<\/p>\n\n\n\n<p>Here&#8217;s a quick overview of the function with examples.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CONVERT ( data_type &#91; ( length ) ] , expression &#91; , style ] ) <\/code><\/pre>\n\n\n\n<p>Where <code>expression<\/code> is the expression to convert, <code>data_type<\/code> is the new data type, and <code>length<\/code> is an optional length for the new data type.<\/p>\n\n\n\n<p>The optional <code>style<\/code> argument can be used to specify how the function should translate the <code>expression<\/code> argument. For example, you could use this argument to specify the date format.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example of converting a string to decimal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT CONVERT(DECIMAL(5,2), '007');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">7.00<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">String to Date<\/h2>\n\n\n\n<p>Here&#8217;s an example of casting a string to a date:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT CONVERT(date, '09 Feb 2030');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">2030-02-09<\/pre>\n\n\n\n<p>In this case the <code>CONVERT()<\/code> function was able to determine which date parts are which because I provided the date in a format that it recognises. <\/p>\n\n\n\n<p>Passing a value that can&#8217;t be cast as a date results in an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><meta charset=\"utf-8\">SELECT CONVERT(date, 'My Birthday');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Msg 241, Level 16, State 1, Line 1\nConversion failed when converting date and\/or time from character string.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Locale<\/h2>\n\n\n\n<p>The language of the current session can make a difference in how dates are interpreted.<\/p>\n\n\n\n<p>Here&#8217;s what happens when using the British language:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET LANGUAGE British;\nSELECT CONVERT(date, '09\/02\/2030');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">2030-02-09<\/pre>\n\n\n\n<p>In this case, the date was interpreted as being the ninth day of February.<\/p>\n\n\n\n<p>Let&#8217;s change the language to <code>us_English<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET LANGUAGE us_English;\n<meta charset=\"utf-8\">SELECT CONVERT(date, '09\/02\/2030');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">2030-09-02<\/pre>\n\n\n\n<p>This time it interpreted the date as being the second day of September.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code><em>style<\/em><\/code> Argument<\/h2>\n\n\n\n<p>We can use the optional <code><em>style<\/em><\/code> argument to specify how the expression should be translated.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET LANGUAGE British;\nSELECT \n    CONVERT(date, '09\/02\/2030') AS \"British\",\n    CONVERT(date, '09\/02\/2030', 101) AS \"US\",\n    CONVERT(date, '09\/02\/30', 1) AS \"US (short)\",\n    CONVERT(date, '20300902', 112) AS \"ISO\",\n    CONVERT(date, '09.02.2030', 104) AS \"German\";<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+------------+------------+--------------+------------+------------+\n| British    | US         | US (short)   | ISO        | German     |\n|------------+------------+--------------+------------+------------|\n| 2030-02-09 | 2030-09-02 | 2030-09-02   | 2030-09-02 | 2030-02-09 |\n+------------+------------+--------------+------------+------------+<\/pre>\n\n\n\n<p>Valid styles:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/database.guide\/date-time-styles-supported-by-convert-in-sql-server\/\" data-type=\"post\" data-id=\"28865\">Styles that can be used with date &amp; time values<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/database.guide\/numeric-styles-supported-by-convert-in-sql-server\/\" data-type=\"post\" data-id=\"28881\">Styles that can be used with numeric values<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/database.guide\/xml-styles-supported-by-convert-in-sql-server\/\" data-type=\"post\" data-id=\"28896\">Styles that can be used when converting to XML values<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/database.guide\/binary-styles-supported-by-convert-in-sql-server\/\" data-type=\"post\" data-id=\"28902\">Styles that can be used when converting to binary values<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">String Concatenation<\/h2>\n\n\n\n<p>Here&#8217;s an example of casting a numeric value to a string in order to concatenate the value with a string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT 'Age: ' + CONVERT(varchar(10), 27);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Age: 27<\/pre>\n\n\n\n<p>Here&#8217;s what happens if we don&#8217;t convert it first:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT 'Age: ' + 27;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Msg 245, Level 16, State 1, Line 1\nConversion failed when converting the varchar value 'Age: ' to data type int.<\/pre>\n\n\n\n<p>When attempting to concatenate two values with different data types, SQL Server needs to implicitly convert one of the values to use the data type of the other, so that they&#8217;re both the same data type before it can perform the concatenation. It follows the rules of <a href=\"https:\/\/database.guide\/data-type-precedence-in-sql-server\/\" data-type=\"post\" data-id=\"22494\">data type precedence<\/a> to do this conversion. <\/p>\n\n\n\n<p>In SQL Server, the <code>int<\/code> data type has a higher precedence than <code>varchar<\/code>. <\/p>\n\n\n\n<p>Therefore, in the above example, SQL Server attempts to convert the string to an integer, but this fails because the string cannot be converted to an integer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Truncating Text<\/h2>\n\n\n\n<p>One handy little trick we can perform with the <code>CONVERT()<\/code> function is to truncate longer strings to a more readable length. <\/p>\n\n\n\n<p>Here&#8217;s an example of truncating text selected from a database table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\n    ProductName,\n    CONVERT(varchar(20), ProductName) AS Truncated\nFROM Products;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+---------------------------------+----------------------+\n| ProductName                     | Truncated            |\n|---------------------------------+----------------------|\n| Left handed screwdriver         | Left handed screwdri |\n| Long Weight (blue)              | Long Weight (blue)   |\n| Long Weight (green)             | Long Weight (green)  |\n| Sledge Hammer                   | Sledge Hammer        |\n| Chainsaw                        | Chainsaw             |\n| Straw Dog Box                   | Straw Dog Box        |\n| Bottomless Coffee Mugs (4 Pack) | Bottomless Coffee Mu |\n| Right handed screwdriver        | Right handed screwdr |\n+---------------------------------+----------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">More Information<\/h2>\n\n\n\n<p>The <code>CONVERT()<\/code> function works in a similar way to the <code><a href=\"https:\/\/database.guide\/how-cast-works-in-sql-server\/\" data-type=\"post\" data-id=\"22481\">CAST()<\/a><\/code> function, and many data conversions can be done using either one. That said, <a href=\"https:\/\/database.guide\/parse-vs-cast-vs-convert-in-sql-server-whats-the-difference\/\" data-type=\"post\" data-id=\"2415\">there are differences between them<\/a>.<\/p>\n\n\n\n<p>See <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/functions\/cast-and-convert-transact-sql\" data-type=\"URL\" data-id=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/functions\/cast-and-convert-transact-sql\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft&#8217;s documentation for <code>CAST()<\/code> and <code>CONVERT()<\/code><\/a> for more detailed information about using this function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SQL Server, the CONVERT() function converts an expression of one data type to another. Here&#8217;s a quick overview of the function with examples.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[72,93,61,20],"class_list":["post-22517","post","type-post","status-publish","format-standard","hentry","category-sql-server","tag-conversion-functions","tag-functions","tag-t-sql","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/22517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=22517"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/22517\/revisions"}],"predecessor-version":[{"id":28908,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/22517\/revisions\/28908"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=22517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=22517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=22517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}