REPLACE Function in SQL

This string function of Structured Query Language replaces all the occurrences of those characters in the original string with the given new character or substring. This function also replaces the value of the column with the new value.

Syntax of REPLACE String Function

Syntax 1: This syntax uses REPLACE function with the column name of the SQL table:

In the syntax, we have to specify the name of that column whose values we want to replace.

Syntax 2: This syntax uses REPLACE function with string:

Syntax 3: This syntax uses REPLACE function with the individual character:

Examples of REPLACE String function

Example 1: The following SELECT query replaces the character 'R' with 'P' in the orginal string:

Output:

Website_Name
JAVATPOINT

Example 2: The following SELECT query replaces all the occurrences of character 'S' with the new character 'T' in the original string:

Output:

Website_Name
JAVATPOINT

Example 3: The following SELECT query replaces the substring 'Tutorials' with the new word 'Articles' in the given original string:

Output:

JavaTpoint_Sentence
JAVATPOINT provides various Articles.

Example 4: The following SELECT query replaces the symbol from the original string with a new symbol:

Output:

Replace_Symbol
$$$$98221545$$

Example 5: The following SELECT query replaces the year in the original string:

Output:

2022JavaTpoint2022   

Example 6: This example uses the REPLACE function with the table in Structured Query Language.

In this example, we have to create a new SQL table through which we will execute the REPLACE() function on columns. The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Student_Marks table:

The below INSERT queries insert the records of college Faculties in the Student_Marks table:

The following SELECT statement displays the inserted records of the above Student_Marks table:


Student_IdStudent_First_NameStudent_Middle_NameStudent_Last_NameStudent_ClassStudent_CityStudent_StateStudent_Marks
4001AmanRoySharma4ChandigarhPunjab88
4002VishalGurrSharma8MurthalHaryana95
4007RajsinghaniaGupta6GhaziabadUttar Pradesh91
4004YashChopraSinghania9JaipurRajasthan85
4011VinaySharmaRoy8ChandigarhPunjab94
4006ManojSinghaniaGupta5GhaziabadUttar Pradesh83
4010RamRaheemGupta9LucknowUttar Pradesh89

Query 1: The following SELECT query uses REPLACE function with the Student_Last_Name column of the above Student_Marks table:

This SQL statement replaces all the occurrences of 'a' with 'r' in the last name of each student:

Output:

Student_Last_NameReplace_a_r
SharmaShrrmr
SharmaShrrmr
GuptaGuptr
SinghaniaSinghrnir
RoyRoy
GuptaGuptr
GuptaGuptr

Query 2: The following SELECT query uses REPLACE function with the Student_City and Student_State column of those students whose Student_Id is greater than 4002 in the above Student_Marks table:

This SQL statement replaces the Chandigarh city and Punjab state of those students whose Student_ID is more than 4002.

Output:


Student_IdREPLACE(Student_City, 'Chandigarh', 'Munnar')REPLACE(Student_State, 'Punjab', 'Kerala )
4007GhaziabadUttar Pradesh
4004JaipurRajasthan
4011MunnarKerala
4006GhaziabadUttar Pradesh
4010LucknowUttar Pradesh





Latest Courses