{"id":943,"date":"2013-06-08T19:21:32","date_gmt":"2013-06-08T13:51:32","guid":{"rendered":"http:\/\/sqlhints.com\/?p=943"},"modified":"2013-07-07T03:06:08","modified_gmt":"2013-07-06T21:36:08","slug":"try_convert-sql-server-2012-built-in-conversion-function","status":"publish","type":"post","link":"https:\/\/sqlhints.com\/2013\/06\/08\/try_convert-sql-server-2012-built-in-conversion-function\/","title":{"rendered":"TRY_CONVERT CONVERSION FUNCTION IN SQL SERVER 2012"},"content":{"rendered":"<p><strong>TRY_CONVERT <\/strong>is one of the new built-in conversion function introduced as a Part of Sql Server 2012.<strong>\u00a0<\/strong>TRY_CONVERT function is similar to the CONVERT \u00a0function, but if\u00a0CONVERT\u00a0function fails to convert the value to the requested type then throws an exception where as TRY_CONVERT function returns NULL value.<\/p>\n<p>Note: \u00a0TRY_CONVERT function raises an exception if we try to an convert expression to a type which is not explicitly permitted.<\/p>\n<p>Syntax: \u00a0TRY_CONVERT (data_type [ ( length ) ], expression [, style ])<\/p>\n<p><strong>Parameter Details:<\/strong><\/p>\n<p><strong>data_type<\/strong>\u00a0 \u00a0 \u00a0: The data type into which to cast expression.<br \/>\n<strong>expression<\/strong>\u00a0 : The value to be cast.<br \/>\n<strong>style<\/strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0: Optional integer value that specifies how the TRY_CONVERT function is to translate expression. The Values for this integer parameter are same as the one used by CONVERT function.<\/p>\n<p><strong>Now let us understand this <strong>TRY_CONVERT\u00a0<\/strong>function with examples:<\/strong><\/p>\n<p><span style=\"color: blue;\"><strong>TRY_CONVERT &#8211; Succeeds<\/strong><\/span><\/p>\n<pre class=\"brush: sql; gutter: true\">SELECT TRY_CONVERT(INT, &#039;100&#039;)\r\nSELECT TRY_CONVERT(NUMERIC(8,2), &#039;1000.06&#039;)\r\nSELECT TRY_CONVERT(INT, 100)\r\nSELECT TRY_CONVERT(NUMERIC(8,2), 1000.06)\r\nSELECT TRY_CONVERT(DATETIME, &#039;05\/18\/2013&#039;)\r\nSELECT TRY_CONVERT(DATETIME, &#039;05\/18\/2013&#039;,111)<\/pre>\n<p><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2013\/06\/TRYCONVERTSUCCEEDS.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-944\" alt=\"TRYCONVERTSUCCEEDS\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2013\/06\/TRYCONVERTSUCCEEDS.jpg\" width=\"366\" height=\"427\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2013\/06\/TRYCONVERTSUCCEEDS.jpg 366w, https:\/\/sqlhints.com\/wp-content\/uploads\/2013\/06\/TRYCONVERTSUCCEEDS-257x300.jpg 257w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/a><\/p>\n<p><span style=\"color: blue;\"><strong>TRY_CONVERT &#8211; Fails<\/strong><\/span><\/p>\n<p><span style=\"color: #333333;\">In the below example we are trying to convert an invalid date i.e. 31st Feburary, 2013. In such cases TRY_CONVERT function will return NULL value instead of throwing exception.<\/span><\/p>\n<pre class=\"brush: sql; gutter: true\">SELECT TRY_CONVERT(DATETIME, &#039;02\/31\/2013&#039;) \r\n            AS &#039;TRY_CONVERT Function Fails&#039;<\/pre>\n<p>Result:<br \/>\nTRY_CONVERT Function Fails<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nNULL<\/p>\n<p><span style=\"color: blue;\"><strong>TRY_CONVERT &#8211; Throws Exception<\/strong><\/span><\/p>\n<p>In the below example we are trying to convert an integer value to XML type which is not permitted explicitly, in such cases TRY_CONVERT function raises an exception.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px;\">SELECT TRY_CONVERT(XML, 10)<\/span><\/p>\n<p>Result:<br \/>\n<span style=\"color: red;\">Msg 529, Level 16, State 2, Line 1<br \/>\nExplicit conversion from data type int to xml is not allowed.<\/span><\/p>\n<p>But on the other-hand conversion of an string value to an XML type is supported. Let us see this with an example:<\/p>\n<pre class=\"brush: sql; gutter: true\">SELECT TRY_CONVERT(XML, &#039;10&#039;) AS &#039;XML Output&#039;<\/pre>\n<p>Result:<\/p>\n<p>XML Output<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n10<\/p>\n<p><span style=\"color: blue;\"><strong>Difference Between CONVERT and TRY_CONVERT<\/strong><\/span><\/p>\n<p>Both CONVERT and TRY_CONVERT function converts the expression to the requested type. But if the CONVERT function fails to convert the value to the requested type then raises an exception, on the other hand if TRY_CONVERT function returns a NULL value if it fails to convert the value to the requested type. Below example demonstrates this difference<\/p>\n<pre class=\"brush: sql; gutter: true\">SELECT CONVERT(DATETIME, &#039;02\/31\/2013&#039;)\r\n            AS &#039;CONVRT Function Result&#039;\r\nSELECT TRY_CONVERT(DATETIME, &#039;02\/31\/2013&#039;) \r\n            AS &#039;TRY_CONVERT Function Result&#039;<\/pre>\n<p>Result:<br \/>\nCONVRT Function Result<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<span style=\"color: red;\">Msg 242, Level 16, State 3, Line 1<br \/>\nThe conversion of a varchar data type to a datetime data type resulted in an out-of-range value.<\/span><\/p>\n<p>TRY_CONVERT Function Result<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nNULL<\/p>\n<p><span style=\"color: blue;\"><strong>Below example demonstrate how we can check the result of TRY_CONVERT function in IF condition:<\/strong><\/span><\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: []; html-script: false\">IF TRY_CONVERT(DATETIME,&#039;Basavaraj&#039;) IS NULL\r\n        PRINT &#039;TRY_CONVERT: Conversion Successful&#039;\r\nELSE\r\n        PRINT &#039;TRY_CONVERT: Conversion Unsuccessful&#039;<\/pre>\n<p><strong>Result:<\/strong><br \/>\nTRY_CONVERT: Conversion Successful<\/p>\n<p>You may like to read the below new built-in function&#8217;s introduced in Sql Server 2012:<\/p>\n<table border=\"1\">\n<tr style=\"border: 1pt solid\">\n<td  style=\"border: 1pt solid windowtext; width: 50%; text-align: center;background-color:#F43722; color:white;\" valign=\"top\" Colspan =\"2\"><strong>New Built in Functions introduced in Sql Server<\/strong><\/td>\n<tr>\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>CONVERSION FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/08\/parse-sql-server-2012-built-in-conversion-function\/\" title=\"PARSE Conversion Function in Sql Server 2012\" target=\"_blank\">PARSE<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/08\/try_parse-sql-server-2012-built-in-conversion-function\/\" title=\"TRY_PARSE Conversion Function in Sql Server 2012\" target=\"_blank\">TRY_PARSE<\/a> <\/strong><\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\" colspan=\"2\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/08\/try_convert-sql-server-2012-built-in-conversion-function\/\" title=\"TRY_CONVERT Conversion Function in Sql Server 2012\" target=\"_blank\">TRY_CONVERT<\/a><\/strong><\/td>\n<tr >\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>STRING FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2012\/12\/02\/concat-function-in-sql-server-2012\/\" title=\"CONCAT String Function in Sql Server 2012\" target=\"_blank\">CONCAT<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/23\/format-string-function-in-sql-server-2012\/\" title=\"FORMAT String Function in Sql Server 2012\" target=\"_blank\">FORMAT<\/a><\/strong<\/td>\n<tr>\n<tr>\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>LOGICAL FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/15\/choose-logical-function-in-sql-server-2012\/\" title=\"CHOOSE Logical Function in Sql Server 2012\" target=\"_blank\">CHOOSE<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/15\/iif-logical-function-in-sql-server-2012\/\" title=\"IIF Logical Function in Sql Server 2012\" target=\"_blank\">IIF<\/a><\/strong<\/td>\n<tr>\n<tr>\n<td style=\"border: 1pt solid windowtext; text-align: center; color: white; background-color:#408080\" colspan=\"2\" valign=\"top\"><strong>DATE AND TIME FUNCTIONS<\/strong><\/td>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\" colspan=\"2\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/22\/eomonth-function-in-sql-server-2012\/\" title=\"EOMONTH Function in Sql Server 2012\" target=\"_blank\">EOMONTH<\/a><\/strong><\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/24\/datefromparts-function-in-sql-server-2012\/\" title=\"DateFromParts Function in Sql Server 2012\" target=\"_blank\">DATEFROMPARTS<\/a><\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/24\/datetimefromparts-function-in-sql-server-2012\/\" title=\"DateTimeFromParts Function in Sql Server 2012\" target=\"_blank\">DATETIMEFROMPARTS<\/a><\/strong<\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong><a href=\"https:\/\/sqlhints.com\/2013\/06\/24\/smalldatetimefromparts-function-in-sql-server-2012\/\" title=\"SmallDateTimeFromParts Function in Sql Server 2012\" target=\"_blank\">SMALLDATETIMEFROMPARTS<\/a> <\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong>DATETIME2FROMPARTS <\/strong<\/td>\n<tr>\n<tr>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong>TIMEFROMPARTS <\/strong><\/td>\n<td style=\"text-align: center; background-color:#D8D8D8\"><strong>DATETIMEOFFSETFROMPARTS<\/strong<\/td>\n<tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>TRY_CONVERT is one of the new built-in conversion function introduced as a Part of Sql Server 2012.\u00a0TRY_CONVERT function is similar to the CONVERT \u00a0function, but if\u00a0CONVERT\u00a0function fails to convert the value to the requested type then throws an exception where as TRY_CONVERT function returns NULL value. Note: \u00a0TRY_CONVERT function raises an exception if we try &hellip; <a href=\"https:\/\/sqlhints.com\/2013\/06\/08\/try_convert-sql-server-2012-built-in-conversion-function\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">TRY_CONVERT CONVERSION FUNCTION IN SQL SERVER 2012<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[89,13,3,5],"tags":[989,91,93,35,986,55,90,92],"class_list":["post-943","post","type-post","status-publish","format-standard","hentry","category-conversion-functions","category-functions","category-sql-server","category-sql-server-2012-sql-server","tag-conversion-functions","tag-convert-vs-try_convert","tag-difference-between-convert-and-try_convert","tag-new-feature-in-sql-server-2012","tag-sql-server","tag-sql-server-2012","tag-try_convert","tag-try_convert-vs-convert"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3xNAz-fd","_links":{"self":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/comments?post=943"}],"version-history":[{"count":31,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/943\/revisions"}],"predecessor-version":[{"id":966,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/943\/revisions\/966"}],"wp:attachment":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/media?parent=943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/categories?post=943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/tags?post=943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}