SQL SERVER查詢正整數(shù)結(jié)果前補(bǔ)0的方法
SQL SERVER查詢的結(jié)果如果為整數(shù)時(shí),如何顯示為00123這樣的格式呢?下面就教您一個(gè)解決此問(wèn)題的方法,希望對(duì)您學(xué)習(xí)SQL SERVER查詢方面能有所幫助。
網(wǎng)上早已很多辦法,這里列出一個(gè)
right('00000'+cast(@count as varchar),5)
其中
'00000'的個(gè)數(shù)為right函數(shù)的***參數(shù),例如這里是5,所以有5個(gè)0
@count就是被格式化的正整數(shù)
同時(shí),給出個(gè)有趣的測(cè)試辦法,在查詢分析里運(yùn)行就能看到結(jié)果
declare @count int
set @count = 0
while (@count < 1000)
begin
print right('00000'+cast(@count as varchar),5)
set @count = @count +1
end
別人的想法很好,值得學(xué)習(xí)!
---------------------------------------------------------sql字符串轉(zhuǎn)換為數(shù)字
declare @dd char(12)
set @dd='00012'
select convert(int,@dd)+1
【編輯推薦】