简体   繁体   English

mysql删除字符串开头的所有空格

[英]mysql remove all whitespace at the beginning of string

So, I realized that for my db column values, I have a whitespace in front of all strings. 因此,我意识到对于我的db列值,所有字符串前面都有一个空格。

(Couldn't figure out why WHERE name = "Sean" wasn't working and by accident, I noticed that there was a whitespace at the front of the value: WHERE name = " Sean" ). (无法弄清楚为什么WHERE name = "Sean"不起作用,并且偶然地,我注意到该值的前面有一个空格: WHERE name = " Sean" )。

There are some values which a space in between words are needed. 在某些值中,单词之间需要有一个空格。

UPDATE (realized that my question was not clear enough). 更新(意识到我的问题还不够清楚)。

I am trying to update the db so that the whitespace at the beginning of each string is removed. 我正在尝试更新数据库,以便删除每个字符串开头的空格。

Thank you. 谢谢。

Try: WHERE ltrim(name) = "Sean" 试试: WHERE ltrim(name) = "Sean"

Link to documentation: http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_ltrim 链接到文档: http : //dev.mysql.com/doc/refman/5.7/zh-CN/string-functions.html#function_ltrim

EDIT: Re-read and saw your edit, and noticed you wanted to update. 编辑:重新阅读并看到您的编辑,并注意到您要更新。 In which case you'd want to run this for each column which has leading whitespace. 在这种情况下,您需要对每个前导空格的列都运行此命令。 Here is an example using a dummy table name, and your name column: 这是一个使用虚拟表名称和您的名称列的示例:

UPDATE table
SET name = ltrim(name)

That will update every row and remove any leading whitespace. 这将更新每一行并删除所有前导空格。

Use LTRIM 使用LTRIM

SELECT LTRIM(name)

Or do an update where you 或在您的位置进行更新

SET name=LTRIM(name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM