
SQL Replace Function Example | Replace Function In SQL is today’s topic. The REPLACE function is used for replacing a sequence of characters in a string with another set of characters, i.e. it is used for replacing all the occurrences of a substring within a present string taken from another new string. The replace function performs case sensitive replacement.
SQL Replace Function
SQL REPLACE() function replaces all the occurrences of the substring within the string, with a new substring.
See the following syntax.
REPLACE(string, old_string, new_string)
- string: The string will be used for replacing the set of characters by another set of characters.
- old_string: The substring which will be searched in the string.
- new_string: All occurrences of old_string will be replaced with new_string in the string.
#Query
SELECT REPLACE(“I Love SQL”, ’I’, ’Everyone’);
See the below output.
Everyone Love SQL
#EXPLANATION
In this example, it is very well clear that I was replaced with Everyone which replaced I love SQL to desired output as shown above.
#Query
Let’s see another example.
SELECT REPLACE(“appdivIdend.com”, ‘i’, ‘3’);
See the output.
appd3vIdend.com
#EXPLANATION
Here, in the above example, i was replaced with 3. As mentioned above, It performs case sensitive replacement. This will happen only in MySQL; in SQL server it is not case sensitive. We have also used the SELECT statement in the above query.
So, in that case, the output would have been appd3v3dend.com
#Query
See the following query.
SELECT REPLACE(“ABC DEF ABC”, ‘a’, ’B’);
See the output.
ABC DEF ABC
#EXPLANATION
Here there was no replacement because “a” was not present in the first string.
#Query
See the following query.
SELECT REPLACE('App Dividend.com', ' ', '_');
See the output.
App_Dividend.com
#EXPLANATION
Here, whitespace or blank space is replaced with an underscore.
#Query
See the following query.
SELECT REPLACE(‘AppDividend.com’, ‘i’, ‘123’);
See the output.
AppD123v123end.com
#EXPLANATION
Here, in the above example, i was replaced with 123.
Now, from the examples mentioned above, you might have got a clear picture of what SQL replace function does.
Let’s look at a proper example where it is used.
Suppose we have a table named STUDENT.
Roll | Name | City | Phone |
1 | Rohit | Patna | 585.145.1475 |
2 | Shouvik | Jalandhar | 225.856.4475 |
3 | Shubh | Kolkata | 123.456.7890 |
4 | Karan | Allahabad | 258.258.1452 |
5 | Shivam | Palampur | 225.142.2589 |
6 | Karan | Dhulian | 145.585.7854 |
Now, if we want to replace the (.) character with (-) character in the phone column. Then we have to use REPLACE function along with UPDATE statement.
#QUERY
UPDATE STUDENT SET Phone = REPLACE(Phone, '.', '-');
See the output.
Roll | Name | City | Phone |
1 | Rohit | Patna | 585-145-1475 |
2 | Shouvik | Jalandhar | 225-856-4475 |
3 | Shubh | Kolkata | 123-456-7890 |
4 | Karan | Allahabad | 258-258-1452 |
5 | Shivam | Palampur | 225-142-2589 |
6 | Karan | Dhulian | 145-585-7854 |
So, here you can see that (.) character is replaced with (-) character.
Finally, SQL Replace Function Example | Replace Function In SQL is over.
The post SQL Replace Function Example | Replace Function In SQL appeared first on AppDividend.