简体   繁体   English

更新表t-sql中的多个值

[英]UPDATE multiple values in table t-sql

I have multiple columns where one of them has varchar values as follow. 我有多个列,其中之一具有以下varchar值。

1
2
4
02
05
6
03
06
123
32
87

I want to find any record that has values starting with 0 > remove that 0 (so 02 > 2) and update that field so there is no value starting with 0. 我想查找具有以0开头的值的任何记录>删除该0(所以02> 2)并更新该字段,所以没有以0开头的值。

How can I go about doing that instead of manually updating each record that has 0 in the front? 我该怎么做,而不是手动更新前面有0的每个记录? Is that even possible? 那有可能吗?

Thank you 谢谢

The following code removes the leading zeros by casting the value to an integer and back to a character string: 以下代码通过将值强制转换为整数并返回字符串来删除前导零:

update t
    set col = cast(cast(col as int) as varchar(255))
    where t.col like '0%'

You can use the following: 您可以使用以下内容:

update yourtable
set col = substring(col, 2, len(col)-1)
where left(col, 1) = '0'

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

Run this query until everything is filtered out I guess... 运行此查询,直到所有内容都被过滤掉为止。

UPDATE Table SET Column=RIGHT(Column, LEN(Column)-1) WHERE Column LIKE '0%';

[Edit] Gordon's approach is probably better. [编辑]戈登的方法可能更好。

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

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