If you REALLY WANT to do it in SQL:declare @tv table( ivh_hdrnumber int NULL)insert @tvselect 7 union allselect 52 union allselect 5923 union allselect 12345678 union allselect 1999999988 select STUFF(convert(char(8), '00000000'), (8 - len(ivh_hdrnumber))+ 1 , len(ivh_hdrnumber), left(cast(ivh_hdrnumber as varchar(8)), len(ivh_hdrnumber))) AS ivh_hdrnumberfrom @tvOutput:ivh_hdrnumber--------------00000007000000520000592312345678NULL
Note that the last entry is null - that's what happens if you have a number larger than 8 digits.