Substitute all occurrences of “T” with “M”.
SELECT REPLACE(‘SQL Tutorial’, ‘T’, ‘M’); |
The REPLACE() function substitutes all instances of a specified substring within a string with a new substring.
Note: The replacement operation is case-insensitive.
Tip: Also consider using the STUFF() function.
REPLACE(string, old_string, new_string) |
Parameter |
Description |
string |
Required: The initial string from which substitutions are made. |
old_string |
Required: The substring within the original string that will be replaced with a new substring. |
new_string |
Required: The string that will replace occurrences of the specified substring in the original string. |
Works in: |
SQL Server (since 2008), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse |
Substitute “SQL” with “HTML”.
SELECT REPLACE(‘SQL Tutorial’, ‘SQL’, ‘HTML’); |
Replace every occurrence of “a” with “c”.
SELECT REPLACE(‘ABC ABC ABC’, ‘a’, ‘c’); |