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

别来无恙 提交于 2019-12-30 05:39:07

问题


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.


回答1:


Check this website

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

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




回答2:


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


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)



回答4:


You are looking for the TRIM function.



来源:https://stackoverflow.com/questions/10964173/mysql-how-can-i-remove-character-at-start-or-end-of-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!