How to convert int to char with leading zeros?

后端 未结 17 868
一生所求
一生所求 2020-12-12 18:55

I need to convert int datafield to nvarchar with leading zeros

example:

1 convert to \'001\'

867 convert to \'000867\', etc.

thx.


17条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 19:17

        select right('000' + convert(varchar(3),id),3) from table
    
        example
        declare @i int
        select @i =1
    
    select right('000' + convert(varchar(3),@i),3)
    

    BTW if it is an int column then it will still not keep the zeros Just do it in the presentation layer or if you really need to in the SELECT

提交回复
热议问题