{"id":1104,"date":"2016-12-06T22:15:36","date_gmt":"2016-12-07T02:15:36","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=1104"},"modified":"2024-04-08T12:50:03","modified_gmt":"2024-04-08T16:50:03","slug":"t-sql-try_convert","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2016\/12\/06\/t-sql-try_convert\/","title":{"rendered":"T-SQL TRY_CONVERT()"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201612\/sql2012.png\" alt=\"SQL Server 2012\" title=\"SQL Server 2012\" style=\"float:left;margin:.5rem;\" \/>I am a little behind the curve on this one.  Starting in SQL Server 2012, Microsoft introduced the TRY_CONVERT() function.  This function works the same way as the CONVERT() function, but if the convert doesn&#8217;t work, you get NULL instead of an error.<\/p>\n<p>Let&#8217;s take a look at the old way:<\/p>\n<pre>\r\nSELECT CONVERT(INT, 'aaa')\r\n<\/pre>\n<p>This returns the message &#8220;<em>Conversion failed when converting the varchar value &#8216;aaa&#8217; to data type int.<\/em>&#8221;  If you are doing a CONVERT() on some column in a large dataset, this often can throw you while you try to sort out what went wrong.  Enter TRY_CONVERT().  Here is the new syntax:<\/p>\n<pre>\r\nSELECT TRY_CONVERT(INT, 'aaa')\r\n<\/pre>\n<p>This just returns NULL and doesn&#8217;t error at all.  In this case, you can easily provide a default value for the conversion by using ISNULL() or COALESCE()<\/p>\n<pre>\r\nSELECT ISNULL(TRY_CONVERT(INT, 'aaa'), 0)\r\n-- OR\r\nSELECT COALESCE(TRY_CONVERT(INT, 'aaa'), 0)\r\n<\/pre>\n<p>Pretty darn awesome and about time (well, even four years ago, it was about time!).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am a little behind the curve on this one. Starting in SQL Server 2012, Microsoft introduced the TRY_CONVERT() function. This function works the same way as the CONVERT() function, but if the convert doesn&#8217;t &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[87],"tags":[4],"class_list":["post-1104","post","type-post","status-publish","format-standard","hentry","category-sql","tag-sql"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/1104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=1104"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/1104\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=1104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=1104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=1104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}