Home >Database >Mysql Tutorial >sql server 格式化字符串加前置0或前导0

sql server 格式化字符串加前置0或前导0

WBOY
WBOYOriginal
2016-06-07 16:19:101606browse

想偷懒找个现成的函数,结果发现写SQL Server的人比我还懒,连这种基础函数都没有! 很好,因为我要的只是流水号而且会每月重置,最大的序数不会超过10万。所以就用rigth吧: declare @SN int declare @SNLen int set @SNLen = 5 set @SN = 1 select right('0

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn