Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql remove very last character [duplicate]

Tags:

string

mysql

you would think this is answered somewhere but I cant find the exact answer. I need to remove the very last character in a string, it can be any character and the string can be any length.

example 992899d needs to be 992899

like image 894
Katherine Pacheco Avatar asked Oct 16 '14 16:10

Katherine Pacheco


1 Answers

Any solution using SUBSTRING would only display the field's content by deleting the last character. It would not actually update the content of the column. If that is what you want(i.e. using it with SELECT) then SUBSTRING is enough.

But, if you want to actually update the value of the column, you can try the following:

UPDATE <table_name>
SET <column_name> = CONCAT(LEFT(<column_name>, CHAR_LENGTH(<column_name>) -1), '')
WHERE <condition>;

This would replace the last character with nothing, hence the last character would be deleted.

Refer: http://dev.mysql.com/doc/refman/5.7/en/string-functions.html

like image 60
Trisha Chatterjee Avatar answered Oct 11 '22 11:10

Trisha Chatterjee