I need to convert int datafield to nvarchar with leading zeros
example:
1 convert to \'001\'
867 convert to \'000867\', etc.
thx.
Use REPLICATE so you don't have to hard code all the leading zeros:
REPLICATE
DECLARE @InputStr int ,@Size int SELECT @InputStr=123 ,@Size=10 PRINT REPLICATE('0',@Size-LEN(RTRIM(CONVERT(varchar(8000),@InputStr)))) + CONVERT(varchar(8000),@InputStr)
OUTPUT:
0000000123