String Functions – REPLACE()


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 “Wizard of OZ?”.

The example below replaces the word “of” with the word “in”.

One thing to always worry about when using TSQL functions is how will it react with empty strings?

Using the empty string as a replacement string results in the search string being removed from the source string.

Using the empty string as a search string results in no change to the input string. Thus, the output string equals the input string.

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.

Next time, I will be exploring the REPLICATE() function.

Related posts

Leave a Comment