The SQL Replace function is used to replace every occurrences of a specified string with a replacement string.
SQL Replace Function Syntax
REPLACE(string_original, string_search, string_replacement)
The SQL Replace function will replace every string_search in string_original with string_replacement.
Note: If any of the string expression is NULL, the function returns NULL. If no match found, the function returns string_original without change.
SQL Replace Function Example
Table: Employees
EmployeeId | FirstName | LastName | Department |
---|---|---|---|
1 | Mazojys | Fxoj | dev |
2 | Jozzh | Lnanyo | dev |
3 | Syllauu | Dfaafk | dev |
4 | Gecrrcc | Srlkrt | dev |
5 | Jssme | Bdnaa | dev |
6 | Dnnaao | Errllov | dev |
7 | Tyoysww | Osk | dev |
Select all the employees, replace dev with development in Department:
SELECT EmployeeId , FirstName , LastName , REPLACE(Department, 'dev', 'development') AS Department FROM Employees
The result will look like:
EmployeeId | FirstName | LastName | Department |
---|---|---|---|
1 | Mazojys | Fxoj | development |
2 | Jozzh | Lnanyo | development |
3 | Syllauu | Dfaafk | development |
4 | Gecrrcc | Srlkrt | development |
5 | Jssme | Bdnaa | development |
6 | Dnnaao | Errllov | development |
7 | Tyoysww | Osk | development |