MySQL mysql删除最后一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26409652/
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:07:51  来源:igfitidea点击:

mysql remove very last character

mysqlstring

提问by Katherine Pacheco

you would think this is answered somewhere but I cant find the exact answer. I need to remove the very last character in a string, it can be any character and the string can be any length.

你会认为这在某处得到了回答,但我找不到确切的答案。我需要删除字符串中的最后一个字符,它可以是任何字符,字符串可以是任何长度。

example 992899d needs to be 992899

例如 992899d 需要是 992899

回答by Trisha Chatterjee

Any solution using SUBSTRINGwould only display the field's content by deleting the last character. It would not actually update the content of the column.If that is what you want(i.e. using it with SELECT) then SUBSTRINGis enough.

任何使用SUBSTRING 的解决方案只会通过删除最后一个字符来显示字段的内容。它实际上不会更新列的内容。如果这就是您想要的(即与SELECT一起使用),那么SUBSTRING就足够了。

But, if you want to actually update the value of the column, you can try the following:

但是,如果要实际更新 column 的值,可以尝试以下操作:

UPDATE <table_name>
SET <column_name> = CONCAT(LEFT(<column_name>, CHAR_LENGTH(<column_name>) -1), '')
WHERE <condition>;

This would replace the last character with nothing, hence the last character would be deleted.

这会将最后一个字符替换为空,因此最后一个字符将被删除。

Refer: http://dev.mysql.com/doc/refman/5.7/en/string-functions.html

参考:http: //dev.mysql.com/doc/refman/5.7/en/string-functions.html

回答by andy

You are probably looking for SUBSTRING(column, 1, CHAR_LENGTH(column)-1).

您可能正在寻找SUBSTRING(column, 1, CHAR_LENGTH(column)-1).

See http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substringfor reference.

请参阅http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring以供参考。

回答by tfoote000

Use TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM ] str)because you already know what section of the string you want to be removed.

使用TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM ] str)是因为您已经知道要删除字符串的哪个部分。

Here is a table with dates and a given quantity that I pulled from my database:

这是我从数据库中提取的包含日期和给定数量的表格:

select date_format(date, '%m/%d %I:%i%p') as Date ...
Date        | QTY
-------------------
3/5 11:30AM | 46
3/5 11:30PM | 23
3/6 11:30AM | 12
...

Using Trim:

使用修剪:

select trim(trailing 'M' from date_format(date, '%m/%d %I:%i%p')) as Date ...
Date        | QTY
-------------------
3/5 11:30A  | 46
3/5 11:30P  | 23
3/6 11:30P  | 12
...

Credit to https://www.w3resource.com/mysql/string-functions/mysql-trim-function.phpfor helping me understand trim

感谢https://www.w3resource.com/mysql/string-functions/mysql-trim-function.php帮助我理解装饰