{"id":5296,"date":"2013-04-16T00:00:06","date_gmt":"2013-04-16T00:00:06","guid":{"rendered":"http:\/\/craftydba.com\/?p=5296"},"modified":"2017-10-11T16:08:13","modified_gmt":"2017-10-11T16:08:13","slug":"string-functions-replace","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5296","title":{"rendered":"String Functions &#8211; REPLACE()"},"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 REPLACE() function today.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186862.aspx\">REPLACE()<\/a> function takes a source string, a search string and a replacement string as input.  For each occurrence of the search string found in the source string, change the value to the replacement string.<\/p>\n<p>Again, I will be using my favorite movie title in this example.  Who does not like the &#8220;Wizard of OZ?&#8221;.  <\/p>\n<p>The example below replaces the word &#8220;of&#8221; with the word &#8220;in&#8221;.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - replace()\">\r\n-- Change one word\r\nselect replace('wizard of oz', 'of', 'in') as str_phrase\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_phrase\r\n------------\r\nwizard in oz\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?  <\/p>\n<p>Using the empty string as a replacement string results in the search string being removed from the source string.  <\/p>\n<p>Using the empty string as a search string results in no change to the input string.  Thus, the output string equals the input string.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - replace()\">\r\n-- Empty string as replacement\r\nselect replace('1 2 3 4 5', ' ', '') as str_no_spaces\r\n\r\n-- Empty string as search returns source string\r\nselect replace('1 2 3 4 5', '', ' ') as str_no_change\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_no_spaces\r\n--------------\r\n12345\r\n\r\nstr_no_change\r\n--------------\r\n1 2 3 4 5\r\n<\/pre>\n<\/p>\n<p>Another thing to worry about when using TSQL functions is how will it react with a NULL values?  Using a NULL value in any of the input parameters results in a output of NULL.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - replace()\">\r\n-- Null returns Null\r\nselect replace(NULL, 'of', 'in') as str_phrase\r\nselect replace('wizard of oz', NULL, 'in') as str_phrase\r\nselect replace('wizard of oz', 'of', NULL) as str_phrase\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nstr_phrase\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\/ms174383.aspx\">REPLICATE()<\/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 REPLACE() function today. The REPLACE() function takes a source string, a search string and a replacement string as input. For each occurrence of the search string found in the source string, change the value to the replacement string. Again, I will be using my favorite movie title in this example. Who does not like the &#8220;Wizard of OZ?&#8221;. The example below replaces the word &#8220;of&#8221; with the word&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,329,815,29],"class_list":["post-5296","post","type-post","status-publish","format-standard","hentry","category-very-short-articles","tag-database-developer","tag-john-f-miner-iii","tag-replace","tag-string-function","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5296","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=5296"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5296\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}