Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

REPLACE

Example

Substitute all occurrences of “T” with “M”.

SELECT REPLACE(‘SQL Tutorial’‘T’‘M’);

Definition and Usage

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.

Syntax

REPLACE(stringold_stringnew_string)

Parameter Values

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.

Technical Details

Works in:

 SQL Server (since 2008), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse

More Examples

Example

Substitute “SQL” with “HTML”.

SELECT REPLACE(‘SQL Tutorial’‘SQL’‘HTML’);

Example

Replace every occurrence of “a” with “c”.

SELECT REPLACE(‘ABC ABC ABC’‘a’‘c’);