{"id":5350,"date":"2013-04-23T00:00:52","date_gmt":"2013-04-23T00:00:52","guid":{"rendered":"http:\/\/craftydba.com\/?p=5350"},"modified":"2017-10-08T16:16:55","modified_gmt":"2017-10-08T16:16:55","slug":"string-functions-str","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5350","title":{"rendered":"String Functions &#8211; STR()"},"content":{"rendered":"<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/04\/turquoise-yarn-md.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-5158\" title=\"turquoise-yarn-md\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/04\/turquoise-yarn-md-150x150.png\" alt=\"\" width=\"150\" height=\"150\" \/><\/a><br \/>\nI am going to continue my series of very short articles or tidbits on Transaction SQL string functions. I will exploring the STR() function today.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms189527.aspx\">STR()<\/a> function takes a floating point number [F] to be converted to a string, a integer number [L] representing the total length, and a integer number [D] representing the number of decimal digits as input.  <\/p>\n<p>It returns a formatted string representing the floating point number.  <\/p>\n<p>The length [L] includes decimal point, sign, numbers and spaces.  The decimal digits [D] represents the number of digits right of the decimal point.<\/p>\n<p>The example below takes a floating point number and rounds it to two decimal places.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - str()\">\r\n-- Format as xxx.xx\r\nselect str(123.456, 6, 2) as str_number;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_number\r\n----------\r\n123.46\r\n<\/pre>\n<\/p>\n<p>One thing to worry about when using TSQL functions is how will it react with out of range values.  In the example below, a negative values are used for total length and number of decimal places.<br \/>\nBoth examples return a NULL string as output.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - str()\">\r\n-- Invalid total length returns NULL\r\nselect str(123.456, -6, 2) as str_number;\r\n\r\n-- Invalid decimal digits returns NULL\r\nselect str(123.456, 6, -2) as str_number;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_number\r\n----------\r\nNULL\r\n<\/pre>\n<\/p>\n<p>Another thing to always worry about when using TSQL functions is how will it react with a EMPTY strings or NULL values?  I was surprised to find out that a empty string returns zero instead of the expected NULL value.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - str()\">\r\n-- Empty string returns 0.00\r\nselect str('', 6, 2) as str_number;\r\n\r\n-- Null returns Null\r\nselect str(NULL, 6, 2) as str_number;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_number\r\n----------\r\n  0.00\r\n\r\nstr_number\r\n----------\r\nNULL\r\n<\/pre>\n<\/p>\n<p>To recap, the STR() function was the only system supplied function for formatting numbers before the FORMAT() function was introduced in SQL Server 2012.  <\/p>\n<p>I will be exploring the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188043.aspx\">STUFF()<\/a> function next time.  Is it Thanksgiving Day already?<\/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 STR() function today. The STR() function takes a floating point number [F] to be converted to a string, a integer number [L] representing the total length, and a integer number [D] representing the number of decimal digits as input. It returns a formatted string representing the floating point number. The length [L] includes decimal point, sign, numbers and spaces. The decimal digits [D] represents the number of digits&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":[31,15,828,815,29],"class_list":["post-5350","post","type-post","status-publish","format-standard","hentry","category-very-short-articles","tag-database-developer","tag-john-f-miner-iii","tag-str","tag-string-function","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5350","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=5350"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5350\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}