BWSnippet

Fun MYSQL

Fun MYSQL

Select field from table that is a varchar, get the last piece, increment that last part of the string, and then re-make the original varchar with new ending!

Say you have a field like "number" and this number is something like 11-000000-001. And you want to increment this.. hmm.. Sounds rough. Well it kinda is if you only want to do it via mysql. I gave it a whirl...

SELECT CONCAT( SUBSTRING(  field , 1 , 10 ) , LPAD( CAST( SUBSTRING( MAX(  field  ) ,  11, 3) AS UNSIGNED )+1 , 3, '0') ) FROM table WHERE a_field=1111

Tags

Fun MYSQL