Welcome to my blog, hope you enjoy reading
RSS

Saturday 1 December 2012

MySQL delete last 4 letters

MySQL delete last 4 letters

UPDATE mytable SET myfield =SUBSTRING(myfield, 1, LENGTH(myfield)-4) ;
If you want to also check whether the field ends with '.php' before truncating, you can add this condition:
UPDATE mytable SET myfield = SUBSTRING(myfield, 1, LENGTH(myfield)-4)
WHERE RIGHT(myfield, 4) = '.php' ;
Oh, there's also LEFT() which can be used instead of SUBSTRING() as well
And CHAR_LENGTH() should be used instead of LENGTH() as it is multi-byte safe (while LENGTH() is not):
UPDATE mytable SET myfield = LEFT(myfield, CHAR_LENGTH(myfield)-4)
WHERE RIGHT(myfield, 4) = '.php' ;

0 comments: