Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql: How can I remove character at start or end of field

Tags:

I have field that contains a comma-separated list. Some old records have a comma at the beginning or end of the field. I need to remove these extra commas.

Example:

,the,their,then to the,their,then

or

the,their,then, to the,their,then

EDIT: I am looking for an UPDATE statement. I need to change the records.

like image 818
user191688 Avatar asked Jun 09 '12 20:06

user191688


People also ask

How do I remove the first character in MySQL?

To cut only the first character, use the substr() function with UPDATE command. The syntax is as follows. UPDATE yourTableName set yourColumnName=substr(yourColumnName,2); To understand the above syntax, let us first create a table.

How do I remove special characters from a MySQL query?

MySQL a-z in Telugu 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 do I remove a character from a column in SQL?

Syntax: SELECT SUBSTRING(column_name,1,length(column_name)-4) FROM table_name; Example : Remove the last 4 characters from the NAME field.

How do I remove leading characters from SQL?

SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string.


2 Answers

Check this website

SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz'); 

which in your case would be ',' instead of 'xyz'

like image 199
codingbiz Avatar answered Dec 02 '22 03:12

codingbiz


@codingbiz, Thank you for the website link:

Quick examples:

SELECT TRIM(BOTH ',' FROM fieldname) from tablename  SELECT TRIM(LEADING ',' FROM fieldname) from tablename  SELECT TRIM(TRAILING ',' FROM fieldname) from tablename 

Thanks!

like image 44
Vijay Avatar answered Dec 02 '22 01:12

Vijay