I have the following table A:
id
----
1
2
12
123
1234
I need to left-pad the id
values with zero\'s:
id
----
0
I needed this in a function on SQL server and adjusted Patrick's answer a bit.
declare @dossierId int = 123
declare @padded_id varchar(7)
set @padded_id = REPLACE(
SPACE(7 - LEN(@dossierId)) + convert(varchar(7), @dossierId),
SPACE(1),
'0')
SELECT @dossierId as '@dossierId'
,SPACE(LEN(@dossierId)) + convert(varchar(7)
,@dossierId) as withSpaces
,@padded_id as '@padded_id'