T-SQL 101: 48 Replacing characters in strings in SQL Server using REPLACE and STUFF
I often need to replace characters in one string with other characters. There are three basic functions in T-SQL for doing that. The most common two functions for this are REPLACE and STUFF.
REPLACE is a simple function. You tell it which string to work on, the substring to find, and which string to replace it with. You can see it here:
I asked it to replace all occurrences of DEF with HELLO. Note that the replacement doesn’t need to be the same length as what it’s replacing. A very common use of REPLACE is to remove characters by making the replacement string just a blank string.
2019-12-16