Wednesday 18 July 2018

Replace function in MySQL

Replace function in MySQL

Like different languages, MySQL too has a Replace() function, which allows you to replace characters.
Syntax: replace(fieldname, ‘find string’, ‘replace with this string’);
Uses:
1. It can be used in select query to display it in the website without permanent change in db.
Syntax: SELECT replace(fieldname, ‘find string’, ‘replace with this string’) AS newfieldname FROM tablename;
Example: SELECT REPLACE( display_name,  ‘é’,  ‘é’ ) FROM  users WHERE  display_name LIKE  ‘%é%’;
2. It can be used in update query for permanent change in db.
Syntax:  UPDATE tablename SET fieldname = replace(fieldname, ‘find string’, ‘replace with this string’) AS newfieldname WHERE CONDITION;  
Example:  UPDATE users SET display_name = REPLACE( display_name,  ‘é’,  ‘é’ ) WHERE  display_name LIKE  ‘%é%’;

0 comments:

Post a Comment