How to convert int to nchar or nvarchar with leadind zeros ?

Posted by Bitnius on Stack Overflow See other posts from Stack Overflow or by Bitnius
Published on 2010-03-07T17:32:13Z Indexed on 2010/03/07 23:53 UTC
Read the original article Hit count: 368

Filed under:
|
|

I need to convert int data table field to nchar or nvarchar leading zeros

example:

1 convert to '001'

867 convert to '000867'

thx.


I tested this T-SQL Script and work fine for me !

DECLARE @number1 INT, @number2 INT

SET @number1 = 1

SET @number2 = 867

SELECT RIGHT('000' + CAST(@number1 AS NVARCHAR(3)), 3 ) AS NUMBER_CONVERTED

SELECT RIGHT('000000' + CAST(@number2 AS NVARCHAR(6)), 6 ) AS NUMBER_CONVERTED

© Stack Overflow or respective owner

Related posts about sql

Related posts about server