I am looking for a way to remove some characters from the end of a field in my MySql database.
Say I have a table called 'items' and there is a field called 'descriptions', some of those descriptions have <br>'s at the end. There could be a single case, or there could be 2-3 <br>'s at the end of that field.
I need to find a way through PHP/MySql to trim off these <br>'s. There may be other <br>'s in the description that I want to keep, it is just the ones at the end I need removed.
I know I can loop through every entry, check for that tag at the end, if it exists strip it off, and then update the DB with the new value. But this seems like the long way of doing it, and I'm not sure how to best achieve what I am trying to do.
Learn MySQL from scratch for Data Science and Analytics You can remove special characters from a database field using REPLACE() function. The special characters are double quotes (“ “), Number sign (#), dollar sign($), percent (%) etc.
Syntax: SELECT SUBSTRING(column_name,1,length(column_name)-N) FROM table_name; Example: Delete the last 2 characters from the FIRSTNAME column from the geeksforgeeks table.
Use the TRIM() function with the LEADING keyword to remove characters at the beginning of a string. TRIM() allows you to remove specific character(s) or space(s) from the beginning, end, or both ends of a string. This function takes the following arguments: An optional keyword that specifies the end(s) to trim.
In order to delete everything after a space, you need to use SUBSTRING_INDEX(). Insert some records in the table using insert command. Display all records from the table using select statement.
I don't have a mysql instance to test, but something like this would probably do it:
UPDATE myTable
SET myCol = TRIM(TRAILING '<br>' FROM myCol);
Take a look at some of the string functions.
You can use the TRIM function in mysql.
UPDATE items SET descriptions = TRIM(TRAILING "<br>" FROM descriptions)
The TRIM usage is here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With