将尾随零添加到SQL中的int字段

时间:2014-09-10 22:58:25

标签: sql sql-server sql-update append concatenation

在重新测序我的网页项目的过程中,我试图在每个项目序列号的末尾添加4个零。我尝试的一切都不起作用,它似乎会因为格式化而自动删除尾随的零。这是一个int字段。作为测试,我唯一需要为1个项目工作的是实际进行替换,例如:

update sacatalogitem
set itm_seq = replace(itm_seq, '8021', '80210000')
where itm_id = '13' and ctg_id = '917'

我尝试使用带有替换的游标,并在执行此操作时删除尾随的零:

declare @SEQ int
declare @ID int
declare @CTG int

declare cur cursor for
    select a.itm_seq, a.itm_id, a.ctg_id from sacatalogitem as a
    join saitem as b on b.itm_id = a.itm_id
    where a.cat_id = '307' and a.itm_id = '13' and a.ctg_id = '917'

open cur

fetch next from cur into @SEQ,@ID,@CTG

While (@@FETCH_STATUS=0)
Begin

Update sacatalogitem
set itm_seq = replace(itm_seq, @SEQ, @SEQ + '0000')
where itm_id = @ID and ctg_id = @CTG

Update saitem
set itm_import_action = 'P'
where itm_id = @ID

fetch next from cur into @SEQ,@ID,@CTG

end

close cur
deallocate cur

这对我来说也失败了:

update sacatalogitem
    set itm_seq = itm_seq + '0000'
    where itm_id = '13' and ctg_id = '917'

对于这个令人沮丧的小问题,任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:2)

如果是整数字段,MULTIPY IT BY 10000.

update sacatalogitem
    set itm_seq = itm_seq * 10000
    where itm_id = '13' and ctg_id = '917'

答案 1 :(得分:0)

你可以尝试

cast(cast (itemsec as varchar) + '0000' as int)

但坦率地说@VJHill的回答更整洁