I'm trying to replace all fields "name" by "name+1" in one update. For example, i want that 2008 become 2009, 2009 become 2010, etc.

I did this for just one replace :

UPDATE table
SET name = REPLACE(name, '2008', '2009');

But if i want to replace now 2009 by 2010, it will be wrong because it will change also 2009 of my last request.

Here is my db :

enter image description here

How can i increment all of these years "name" in one request without conflict and without start by the latest year ?

Thanks for your time.

2 Answers

Barbaros Özhan On Best Solutions

Just use casting ( as unsigned for numeric type )

update table
   set name = cast(name as unsigned) + 1;

considering your column(name) contains integer year values only.

forpas On

As long as the column value is purely numeric, even if its data type is varchar, you don't need any casting.
MySql does implicit casting:

update tablename
set name = name + 1;

See the demo.