I want to add the string ,'2') to a row in my database. The current string in the database looks like this: ('1','42','2122','3')
If I was to add it normally, this would be the result: ('1','42','2122','3'),'2').
How can I delete the last character of the string, that is already in the database, so it will end up like this: ('1','42','2122','3','2')
I tried using concat and (sub)str_replace, but that did not give me the desired result.
Is this possible using SQL only, without first fetching the record and updating it?
Well, you can use the replace function of mysql
This function is pretty much similar to ‘Find-Replace’ dialog box found in editors. This would replace all occurrences of ‘FIND_String’ with ‘REPLACE_String’ in ‘String_Expression’
Example –
Select REPLACE ('Vipin', 'i', 'X')Output – VXpXn
Syntax:
Replace ( 'String_Expression', 'FIND_String', 'REPLACE_String' )docs: http://www.electrictoolbox.com/mysql-select-replace/
In your case, if you have only one closing bracket then you can use the following query to update you table..
PHP code
Well keep in mind this will replace every instance of
')'to$newStringso this solution will work only if there are only one closing bracket in the end.