How do I convert an int to a zero padded string in T-SQL?
Let's say I have an int with the value of 1. How can I convert that int to a zero padded string, such as 00000001
?
Solution 1:
Declare @MyInt integer Set @MyInt = 123
Declare @StrLen TinyInt Set @StrLen = 8
Select Replace(Str(@MyInt, @StrLen), ' ' , '0')
Solution 2:
Another way is:
DECLARE @iVal int = 1
select REPLACE(STR(@iVal, 8, 0), ' ', '0')