Reputation: 16167
I need to update values in a table by removing their last char if they ends with a +
Example:
John+Doe
and John+Doe+
should both become John+Doe
.
What's the best way to achieve this?
Upvotes: 4
Views: 7462
Reputation: 149
if you are passing to the DB as a string, you can do this with str_replace
<?php
$str = "John+Doe+";
$str = str_replace("+"," ",$str);
echo $str;
?>
Upvotes: 0
Reputation: 284
you didn't explain exactly the situation. but if you search for names in a text. I'll remove all the non chars (anything not a-z and A-Z) including spaces and then compare.
if you want just the last char, try the SUBSTRING_INDEX function.
Upvotes: 0
Reputation: 62831
If you are trying to display the field instead of update the table, then you can use a CASE
statement:
select
case
when right(yourfield,1) = '+' then left(yourfield,length(yourfield)-1)
else yourfield end
from yourtable
Upvotes: 1
Reputation: 44581
UPDATE table
SET field = SUBSTRING(field, 1, CHAR_LENGTH(field) - 1)
WHERE field LIKE '%+'
Upvotes: 14