tags:

views:

376

answers:

3

I need a SQL update statement for updating a particular field of all the rows with a string "test" join in the front of the existing value

For example, if the existing value is try it became testtry

A: 

That's a simple one

UPDATE YourTable SET YourColumn = CONCAT('prependedString', YourColumn);
soulmerge
Correct up to the WHERE clause, where you only concat test to columns already starting with test.So:foo -> foofootest -> footesttestfoo -> testtestfoo
Jukka Dahlbom
Thanks, guess I got the question wrong :)
soulmerge
+6  A: 

You can use the CONCAT function to do that:

UPDATE tbl SET col=CONCAT('test',col);

If you want to get cleverer and only update columns which don't already have test prepended, try

UPDATE tbl SET col=CONCAT('test',col)
WHERE col NOT LIKE 'test%';
Paul Dixon
+1  A: 
UPDATE tablename SET fieldname = CONCAT("test", fieldname) [WHERE ...]
Ferdinand Beyer