{"id":5177,"date":"2013-04-04T00:00:06","date_gmt":"2013-04-04T00:00:06","guid":{"rendered":"http:\/\/craftydba.com\/?p=5177"},"modified":"2017-10-11T16:28:31","modified_gmt":"2017-10-11T16:28:31","slug":"string-function-char","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5177","title":{"rendered":"String Function &#8211; CHAR()"},"content":{"rendered":"<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/04\/turquoise-yarn-md.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/04\/turquoise-yarn-md-150x150.png\" alt=\"\" title=\"turquoise-yarn-md\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-5158\" \/><\/a><br \/>\nI am going to continue my series of very short articles or tidbits on Transaction SQL string functions.  I will exploring the CHAR() function today.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms187323.aspx\">CHAR()<\/a> function takes an integer value, ASCII code, from 0 to 255 and returns a single character string.<\/p>\n<p>The following example returns the character &#8216;0&#8217; given the ASCII code of 48.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - char()\">\r\n-- ASCII code for zero\r\ndeclare @var_tmp int = 48;\r\n\r\n-- Return the single character\r\nselect char(@var_tmp) as single_char;\r\n<\/pre>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput:\r\n\r\nsingle_char\r\n-----------\r\n0\r\n<\/pre>\n<\/p>\n<p>One thing to always worry about when using TSQL functions is how will it react with a UNKNOWN or NULL values? <\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - char()\">\r\n-- UNKNOWN value returns NULL\r\nselect char(NULL) as single_char;\r\n<\/pre>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput:\r\n\r\nsingle_char\r\n-----------\r\nNULL\r\n<\/pre>\n<\/p>\n<p>Another thing to test is how the function will work with values out of range?<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - char()\">\r\n-- Out of range values return NULL\r\nselect char(-1) as single_char;\r\nselect char(512) as single_char;\r\n<\/pre>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput:\r\n\r\nsingle_char\r\n-----------\r\nNULL\r\n<\/pre>\n<\/p>\n<p>Next time, I will be exploring the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186323.aspx\">CHARINDEX()<\/a> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going to continue my series of very short articles or tidbits on Transaction SQL string functions. I will exploring the CHAR() function today. The CHAR() function takes an integer value, ASCII code, from 0 to 255 and returns a single character string. The following example returns the character &#8216;0&#8217; given the ASCII code of 48. &#8212; ASCII code for zero declare @var_tmp int = 48; &#8212; Return the single character select char(@var_tmp) as single_char; output: single_char &#8212;&#8212;&#8212;&#8211; 0 One thing to always worry about when using TSQL functions&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[531,31,15,815,29],"class_list":["post-5177","post","type-post","status-publish","format-standard","hentry","category-very-short-articles","tag-char","tag-database-developer","tag-john-f-miner-iii","tag-string-function","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5177","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=5177"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5177\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}