{"id":5363,"date":"2013-04-25T00:00:46","date_gmt":"2013-04-25T00:00:46","guid":{"rendered":"http:\/\/craftydba.com\/?p=5363"},"modified":"2017-10-08T16:18:20","modified_gmt":"2017-10-08T16:18:20","slug":"string-functions-substring","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5363","title":{"rendered":"String Functions &#8211; SUBSTRING()"},"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 SUBSTRING() function today.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms187748.aspx\">SUBSTRING() <\/a>function allows a developer to slice and dice a string into pieces.  It takes a source string [T], a integer starting position [S], and a the number of characters [C] to return as input parameters.  It returns a string of [C] characters from position [S] as output.<\/p>\n<p>The example below extracts one word from a famous movie title.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - substring()\">\r\n-- Extract one word\r\nselect substring('wizard of oz', 8, 2) as str_word\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_word\r\n--------\r\nof\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.  A negative starting position [S] returns an empty string as output.  A negative number of characters causing a system error.  Please see the examples below.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - substring()\">\r\n-- Invalid start parameter return empty strings\r\nselect substring('wizard of oz', -8, 2) as str_word\r\n\r\n-- Invalid length parameter\r\nselect substring('wizard of oz', 8, -2) as str_word\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_word\r\n--------\r\n\r\nMsg 536, Level 16, State 1, Line 3\r\nInvalid length parameter passed to the substring function.\r\n\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?  A empty string as input results in a empty string as output.  I was surprised to find out that a NULL string as input generates a system error.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - substring()\">\r\n-- Empty string returns itself\r\nselect substring('', 8, 2) as str_word\r\n\r\n-- NULL string is invalid\r\nselect substring(NULL, 8, 2) as str_word\r\n\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_word\r\n--------\r\n\r\nMsg 8116, Level 16, State 1, Line 2\r\nArgument data type NULL is invalid for argument 1 of substring function.\r\n\r\n<\/pre>\n<\/p>\n<p>To recap, be careful when using this function.  A invalid length [C] or NULL input string [T] will result in a system error.  On the other hand, this function is great if you need to slice and dice a string.<\/p>\n<p>I will be blogging about the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms180059.aspx\">UNICODE()<\/a> function next time.  <\/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 SUBSTRING() function today. The SUBSTRING() function allows a developer to slice and dice a string into pieces. It takes a source string [T], a integer starting position [S], and a the number of characters [C] to return as input parameters. It returns a string of [C] characters from position [S] as output. The example below extracts one word from a famous movie title. &#8212; Extract one word select&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,815,614,29],"class_list":["post-5363","post","type-post","status-publish","format-standard","hentry","category-very-short-articles","tag-database-developer","tag-john-f-miner-iii","tag-string-function","tag-substring","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5363","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=5363"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5363\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}