How to convert int to char with leading zeros?

后端 未结 17 879
一生所求
一生所求 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:20

    I like to use

    DECLARE @Length int
    DECLARE @Number int
    SET @Length = 9
    SET @Number = 4
    
    select right( POWER(10, @Length) + @Number, @Length)
    

    this gives me

    000000004
    

提交回复
热议问题