Using MySql replace function

MySql has a very useful function replace() which allows you to replace substring or characters. It is really useful while working with huge database table where you want to update certain values with another value. Its syntax is quite simple.

   REPLACE ( field_name, 'find_string', 'replacement_string' );

To update some values in database table use query as follows using replace function.

   UPDATE tutorials SET topic = REPLACE (topic, 'Php', 'Php MySql');

You can also run a select query using this function if you don't want to update table but want to fetch data as replaced. This will only output replaced values and there will be no changes in database.

   SELECT REPLACE (topic, 'Php', 'Php MySql') as new_topic 
   FROM tutorials 
   WHERE topic = 'Php';

Using MySql replace function Using MySql replace function Reviewed by Altaf Hussain on January 14, 2014 Rating: 5

No comments:

Altaf Web. Powered by Blogger.