Monday, September 23, 2013

MYSQL UPDATE WITH REPLACE() FUNCTION

Hiii, you all know the basic sql queries like insert,update etc but majority of us are unaware of many useful functions that mysql provides.
Lets take an example that if you want to update a column value – “www.example.com” in a table to value – “localhost/example”. then you will simply use a update query as :
update tablename set columnnname=”localhost/example” where =”www.example.com”;
and your work will be done but what if  you want to replace -”www.example.com” in column having value- -”www.example.com/categories” with value -”localhost/example” to make the final value of column look like – “localhost/example/categories”……..this can’t be useful if records are very large like if you want to setup a wordpress site running live to your local system…….then this replacement with updation is done by mysql replace() function .
mysql

replace() function syntax :

replace(columnname,”value to be replace”,”value to be replace with”);

replace() function usage :

UPDATE tablename SET columnname = REPLACE(columnname, 'http://www.example.com', ‘localhost/example’);
the above mentioned query replaces every  'http://www.example.com' with ‘localhost/example’ in that column whose name is mentioned as columnname in that particular table mentioned in tablename.
Hope you like this post……..Please Comment....!!!!!!
Originally posted at Mycodestock.


Tags: , ,

0 Responses to “MYSQL UPDATE WITH REPLACE() FUNCTION”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks