Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove characters from end of field in MySql database

Tags:

php

mysql

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.

like image 343
Sherwin Flight Avatar asked Apr 10 '12 03:04

Sherwin Flight


People also ask

How do I remove special characters from a MySQL query?

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.

How can I remove last 5 characters from a string in SQL?

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.

How do I trim a character in MySQL?

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.

How do I delete everything after a character in MySQL?

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.


2 Answers

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.

like image 175
Glenn Avatar answered Oct 05 '22 17:10

Glenn


You can use the TRIM function in mysql.

UPDATE items SET descriptions = TRIM(TRAILING "<br>" FROM descriptions)

The TRIM usage is here.

like image 27
yaronli Avatar answered Oct 05 '22 17:10

yaronli