想偷懶找個現成的函數,結果發現寫SQL Server的人比我還懶,連這種基礎函數都沒有!
很好,因為我要的只是流水號而且會每月重置,最大的序數不會超過10萬。所以就用rigth吧:
declare @SN int
declare @SNLen int
set @SNLen = 5
set @SN = 1
select right('0000000000' + cast(@SN as varchar(10)), @SNLen)
set @SN = 333
select right('0000000000' + cast(@SN as varchar(10)), @SNLen)
set @SN = 55555
select right('0000000000' + cast(@SN as varchar(10)), @SNLen)
這些代碼可能會相關:
declare @OfYear nvarchar(4)
declare @OfMonth nvarchar(2)
set @OfYear = datename(yyyy, @DateTime)
set @OfMonth = datename(mm, @DateTime)
Yes, right