2014-09-10 38 views
0

在重新排序我的Web項目的過程中,我試圖在每個項目序號的末尾附加4個零。我所嘗試的一切都不起作用,它似乎會自動刪除格式化後的零。這是一個int字段。我到了1項作爲測試工作的唯一的東西實際上是做了更換,例如:將尾隨零添加到SQL中的int字段中

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' 

任何幫助,將不勝感激這個小令人沮喪的問題。

+0

將你想要什麼,如果itm_seq爲零( 0或00000)? – hatchet 2014-09-10 23:18:24

回答

2

如果它是一個整型字段,MULTIPY IT BY 10000.

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

除非itm_seq爲零 – hatchet 2014-09-10 23:13:55

+0

工作正常。感謝@VJHil的反饋! – 2014-09-11 02:38:14

0

你可以嘗試

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

但坦率地說@VJHill的回答是簡潔