{"id":5397,"date":"2013-04-24T00:00:28","date_gmt":"2013-04-24T00:00:28","guid":{"rendered":"http:\/\/craftydba.com\/?p=5397"},"modified":"2017-10-08T16:17:40","modified_gmt":"2017-10-08T16:17:40","slug":"string-functions-stuff","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5397","title":{"rendered":"String Functions &#8211; STUFF()"},"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 STUFF() function today.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188043.aspx\">STUFF()<\/a> function takes a source string [S], a starting position [P], a total length [L] and a replacement string [R] as input parameters. It removes [L] characters from the source string [S] starting at position [P] and replaces those changes with string [R].  <\/p>\n<p>From the definition of this function, one could easily write a custom user defined function with the same functionality using the SUBSTRING() function.  This function has been part of the core language since SQL Server 2005.<\/p>\n<p>The following example converts the favorite movie title of mine to upper case.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - stuff()\">\r\n-- Correct call to function\r\nSELECT \r\n    STUFF('Pay no attention to the man behind the curtain!', 5, 2, \r\n          'alot of') as oz_quote;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\noz_quote\r\n------------\r\nPay alot of attention to the man behind the curtain!\r\n<\/pre>\n<\/p>\n<p>When a NULL value or empty string is used for the replacement string [R], the stuff function just removes the characters from the source string [S].<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - stuff()\">\r\n-- With expression - null & empty = remove characters\r\nSELECT \r\n    STUFF('Pay no attention to the man behind the curtain!', \r\n          4, 3, '') as oz_quote;\r\n\r\nSELECT \r\n    STUFF('Pay no attention to the man behind the curtain!', \r\n          4, 3, NULL) as oz_quote;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\noz_quote\r\n------------\r\nPay attention to the man behind the curtain!\r\n<\/pre>\n<\/p>\n<p>One thing to always worry about when using TSQL functions is how will it react with empty strings or NULL values?  Both empty strings and NULL values as input return NULL as output.  <\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - stuff()\">\r\n-- Invalid start & length = null\r\nSELECT \r\n    STUFF('Pay no attention to the man behind the curtain!', \r\n          NULL, 3, 'alot of') as oz_quote;\r\n\r\nSELECT \r\n    STUFF('Pay no attention to the man behind the curtain!', \r\n          4, NULL, 'alot of') as oz_quote;\r\n\r\n-- Source expression - null & empty = null\r\nSELECT STUFF('' , 5, 2, 'alot of') as oz_quote;\r\n\r\nSELECT STUFF(NULL , 5, 2, 'alot of') as oz_quote;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\noz_quote\r\n------------\r\nNULL\r\n<\/pre>\n<\/p>\n<p>To recap, if you need to replace one occurrence of a string [X] contained within string [S] with string [R], consider using the STUFF() function.  If you need to replace multiple occurrences, use the REPLACE() function.  Next time, I will be playing with the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms187748.aspx\">SUBSTRING()<\/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 STUFF() function today. The STUFF() function takes a source string [S], a starting position [P], a total length [L] and a replacement string [R] as input parameters. It removes [L] characters from the source string [S] starting at position [P] and replaces those changes with string [R]. From the definition of this function, one could easily write a custom user defined function with the same functionality using the&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,832,29],"class_list":["post-5397","post","type-post","status-publish","format-standard","hentry","category-very-short-articles","tag-database-developer","tag-john-f-miner-iii","tag-string-function","tag-stuff","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5397","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=5397"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5397\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}