{"id":6064,"date":"2013-07-09T00:00:38","date_gmt":"2013-07-09T00:00:38","guid":{"rendered":"http:\/\/craftydba.com\/?p=6064"},"modified":"2016-04-21T21:28:26","modified_gmt":"2016-04-21T21:28:26","slug":"conversion-functions-try_cast","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=6064","title":{"rendered":"Conversion Functions &#8211; TRY_CAST()"},"content":{"rendered":"<p>I am going to make headway on my series of articles (tidbits) on type conversion functions available in the Transaction SQL (T-SQL) language.<\/p>\n<p>One analogy of a data <a href=\"http:\/\/en.wikipedia.org\/wiki\/Type_conversion\">type conversion<\/a> is apply the correct action to change the physical form of water (H2O).  You can convert water to ice by cooling it.  You can convert water to vapor by heating it.  However, the chemical composition of water is still the same!<\/p>\n<p>Information is stored in a database as rows in a table.  Each field in a table is defined with a certain data type.  The original designer of the database schema might not be aware of all potential uses of the stored information.  Therefore, it is not uncommon as a developer to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Type_conversion\">convert data<\/a> from one type to another. <\/p>\n<p>Today, I want to talk about how to use <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh974669.aspx\">TRY_CAST<\/a> function which takes a source data value and a target data type as parameters. It returns the the source data value casted to the target data type.  If the data type conversion is not allowed or any potential ERROR occurs, the function returns a NULL value.<\/p>\n<p>The same examples are used again for researching this function.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"try_cast() - conversion functions\">\r\n--\r\n-- Format variables using TRY_CAST()\r\n--\r\n\r\n-- Famous quote\r\nDECLARE @VAR_QUOTE VARCHAR(128) = \r\n'It is the mark of an educated mind to be able to \r\nentertain a thought without accepting it - Aristotle';\r\n\r\n-- Full quote\r\nSELECT @VAR_QUOTE AS FULL_QUOTE;\r\n\r\n-- Unexpected truncation\r\nSELECT TRY_CAST(@VAR_QUOTE AS CHAR(34)) AS SHORT_QUOTE;\r\n\r\n-- My 64 K number\r\nDECLARE @VAR_NUMBER INT = POWER(2, 16)\r\n\r\n-- Full number\r\nSELECT @VAR_NUMBER AS LARGE_NUMBER\r\n\r\n-- Returns NULL\r\nSELECT TRY_CAST(@VAR_NUMBER AS SMALLINT) AS SHORT_NUMBER\r\n\r\n-- Date as hex numbers\r\nDECLARE @VAR_BYTE AS BINARY(4) = 0x20130723;\r\n\r\n-- Returns a date\r\nSELECT TRY_CAST('20130723' as DATE) AS RIGHT_DATE\r\n\r\n-- Returns NULL\r\nSELECT TRY_CAST(@VAR_BYTE as DATE) AS WRONG_DATE\r\n\r\n<\/pre>\n<\/p>\n<p>The output from the TSQL example is shown below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nFULL_QUOTE\r\n-----------------------------------------------------\r\nIt is the mark of an educated mind to be able to \r\nentertain a thought without accepting it - Aristotle\r\n\r\nSHORT_QUOTE\r\n----------------------------------\r\nIt is the mark of an educated mind\r\n\r\nLARGE_NUMBER\r\n------------\r\n65536\r\n\r\nSHORT_NUMBER\r\n------------\r\nNULL\r\n\r\nRIGHT_DATE\r\n----------\r\n2013-07-23\r\n\r\nWRONG_DATE\r\n----------\r\nNULL\r\n\r\n<\/span><\/pre>\n<\/p>\n<p>Unlike the CAST function, we end up with a NULL values when arithmetic overflow occurs in statement four or an invalid conversion happens in statement six.  <\/p>\n<p>In summary, I suggest using the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh974669.aspx\">TRY_CAST<\/a> with the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms184325.aspx\">ISNULL<\/a> function to prevent run time errors in your code.<\/p>\n<p>Next time, I will be talking about the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh213126.aspx\">TRY_PARSE<\/a> function.  This function was introduced in SQL Server 2012.  Unlike its cousin, invalid casting of data results in a NULL value instead of an ERROR.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going to make headway on my series of articles (tidbits) on type conversion functions available in the Transaction SQL (T-SQL) language. One analogy of a data type conversion is apply the correct action to change the physical form of water (H2O). You can convert water to ice by cooling it. You can convert water to vapor by heating it. However, the chemical composition of water is still the same! Information is stored in a database as rows in a table. Each field in a table is defined with&hellip;<\/p>\n","protected":false},"author":1,"featured_media":6008,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[892,893,31,895,894,330,15,28,899,29,891],"class_list":["post-6064","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-very-short-articles","tag-conversion-functions","tag-conversion-matrix","tag-database-developer","tag-explicit-conversion","tag-implicit-conversion","tag-isnull","tag-john-f-miner-iii","tag-sql-server","tag-try_cast","tag-tsql","tag-type-conversion"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/6064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6064"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/6064\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/media\/6008"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}