mysql:如何删除字段开头或结尾的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10964173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:45:53  来源:igfitidea点击:

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

mysqlreplace

提问by user191688

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,thento the,their,then

,the,their,thenthe,their,then

or

或者

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

the,their,then,the,their,then

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

编辑:我正在寻找 UPDATE 语句。我需要更改记录。

回答by codingbiz

Check thiswebsite

检查这个网站

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

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

在您的情况下,它将是 ',' 而不是 'xyz'

回答by Vijay

@codingbiz, Thank you for the website link:

@codingbiz,感谢您提供网站链接:

Quick examples:

快速示例:

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

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

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

Thanks!

谢谢!

回答by Tegan Snyder

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)

回答by RandomSeed

You are looking for the TRIMfunction.

您正在寻找TRIM功能