Replaces all occurrences of a specified string value with another string value.
Transact-SQL Syntax Conventions
REPLACE ( string_expression , string_pattern , string_replacement )
Is the string expression to be searched. string_expression can be of a character or binary data type.
Is the substring to be found. string_pattern can be of a character or binary data type. string_pattern cannot be an empty string ('').
Is the replacement string. string_replacement can be of a character or binary data type.
Returns nvarchar if one of the input arguments is of the nvarchar data type; otherwise, REPLACE returns varchar.
Returns NULL if any one of the arguments is NULL.
REPLACE performs comparisons based on the collation of the input. To perform a comparison in a specified collation, you can use COLLATE to apply an explicit collation to the input.
The following example replaces the string cde in abcdefghi with xxx.
cde
abcdefghi
xxx
SELECT REPLACE('abcdefghicde','cde','xxx'); GO
Here is the result set.
------------ abxxxfghixxx (1 row(s) affected)
The following example uses the COLLATE function.
COLLATE
SELECT REPLACE('Das ist ein Test' COLLATE Latin1_General_BIN, 'Test', 'desk' ); GO
------------ Das ist ein desk (1 row(s) affected)