简体   繁体   中英

UPDATE multiple values in table t-sql

I have multiple columns where one of them has varchar values as follow.

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.

How can I go about doing that instead of manually updating each record that has 0 in the front? 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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