Fast padding int values with zeros in MS SQL
2010-Лют-09, Вівторок 15:02
I had a zip code as int and needed it as 4 chars length string with leading zeros.
That's a simplified example how it was done.
select top 5
IDStr = RIGHT(REPLICATE('0',4)+CONVERT(varchar(4),tt.ID),4),
IDInt = tt.ID
from Publication tt
IDStr IDInt
----- -----------
0001 1
0002 2
0003 3
0004 4
0005 5
(5 row(s) affected)