想偷懒找个现成的函数,结果发现写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..