SQL 如何添加字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4481653/
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
How to add a character
提问by Bob
I have a table like this:
我有一张这样的表:
Item Code
A 123456
B 123455
C 23457
D 123458
E 23459
F
The Code
column must have 6 characters and I need to add '1' (for example, 23455
to 123455
) for those items with less than 6 characters.
该Code
列必须有 6 个字符,我需要为那些少于 6 个字符的项目添加 '1'(例如,23455
to 123455
)。
How can I do it with SQL ?
我怎样才能用 SQL 做到这一点?
Thanks,
谢谢,
回答by Paul Morgan
Update table
set Code = CONCAT( '1', TRIM( Code ) )
where LEN( TRIM( CODE ) ) < 6
回答by bobs
For SQL Server and assuming the Code column is a character data type, you can do the following
对于 SQL Server 并假设 Code 列是字符数据类型,您可以执行以下操作
UPDATE myTable
SET Code = '1' + Code
WHERE LEN(Code) < 6