REPLACE Function in SQLThis 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 FunctionSyntax 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 functionExample 1: The following SELECT query replaces the character 'R' with 'P' in the orginal string: Output:
Example 2: The following SELECT query replaces all the occurrences of character 'S' with the new character 'T' in the original string: Output:
Example 3: The following SELECT query replaces the substring 'Tutorials' with the new word 'Articles' in the given original string: Output:
Example 4: The following SELECT query replaces the symbol from the original string with a new symbol: Output:
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:
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:
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:
Next TopicSUBSTR Function in SQL |