How to convert int to char with leading zeros?

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

    Not very elegant, but I add a set value with the same number of leading zeroes I desire to the numeric I want to convert, and use RIGHT function.

    Example:

    SELECT RIGHT(CONVERT(CHAR(7),1000000 + @number2),6)
    

    Result: '000867'

提交回复
热议问题