{"id":6025,"date":"2013-07-06T00:00:26","date_gmt":"2013-07-06T00:00:26","guid":{"rendered":"http:\/\/craftydba.com\/?p=6025"},"modified":"2016-04-21T21:33:48","modified_gmt":"2016-04-21T21:33:48","slug":"conversion-functions-cast","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=6025","title":{"rendered":"Conversion Functions &#8211; CAST()"},"content":{"rendered":"<p>I am going press on with 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:\/\/msdn.microsoft.com\/en-us\/library\/ms187928.aspx\">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.<\/p>\n<p>The TSQL example below shows valid and invalid conversions for strings, numbers, and dates.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"cast() - conversion functions\">\r\n--\r\n-- Format variables using 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 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-- Overflow when converting  \r\nSELECT 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-- Conversion is allowed\r\nSELECT CAST('20130723' as DATE) AS RIGHT_DATE\r\n\r\n-- Conversion is not allowed\r\nSELECT 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\nMsg 220, Level 16, State 1, Line 8\r\nArithmetic overflow error for data type smallint, value = 65536.\r\n\r\nRIGHT_DATE\r\n----------\r\n2013-07-23\r\n\r\nWRONG_DATE\r\n----------\r\nMsg 241, Level 16, State 1, Line 3\r\nConversion failed when converting date and\/or time from character string.\r\n\r\n<\/pre>\n<\/p>\n<p>Here are the results from executing the TSQL example.  <\/p>\n<ol>\n<li>\nThe large string is converted to a smaller string.  Truncation occurs as a side effect of the operation.  No warning is produced.\n<\/li>\n<li>\nI am storing 2 raised to the 16th power as a integer.  Casting the larger number to a smaller number results in a arithmetic overflow.  A error is raised because of this operation.\n<\/li>\n<li>\nI am able to cast a string to a date.  However, converting the date stored as a 4 byte hex code to a date data type fails.  A error is raised because this conversion is not allowed.\n<\/li>\n<\/ol>\n<p>Please note the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms187928.aspx\">CAST<\/a> function uses the following <a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/07\/sql-server-conversion-matrix.gif\">matrix<\/a> to transform data from one form to another.  <\/p>\n<p>Next time, I will be talking about the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh213316.aspx\">PARSE<\/a> function that takes a source string, a target data type, and a optional culture information as parameters.  It returns the target data type containing the transformed source string.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going press on with 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 a&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":[896,892,893,31,895,894,15,28,29,891],"class_list":["post-6025","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-very-short-articles","tag-cast","tag-conversion-functions","tag-conversion-matrix","tag-database-developer","tag-explicit-conversion","tag-implicit-conversion","tag-john-f-miner-iii","tag-sql-server","tag-tsql","tag-type-conversion"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/6025","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=6025"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/6025\/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=6025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}