mysql: How can I remove character at start or end

2020-03-01 04:12发布

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.

标签: mysql replace
4条回答
够拽才男人
2楼-- · 2020-03-01 04:20

@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!

查看更多
虎瘦雄心在
3楼-- · 2020-03-01 04:27

You are looking for the TRIM function.

查看更多
Animai°情兽
4楼-- · 2020-03-01 04:29

Check this website

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

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

查看更多
劳资没心,怎么记你
5楼-- · 2020-03-01 04:31

The question was how to remove the leading and trailing characters I will show you an example with an update query.

UPDATE your_table_name
SET your_record_name = TRIM(BOTH ',' FROM your_record_name)
查看更多
登录 后发表回答