create function [dbo].[PadLeft]
(
@input int
,@width int
,@pad char(1)
) returns varchar(max) as begin
/*
declare
@input int = 1
,@width int = 3
,@pad char(1) = '0'
*/
declare @len int = case when @input = 0 then 1 else ceiling(LOG10(@input+1)) end
declare @result varchar(max) = REPLICATE(@pad, @width - @len) + CONVERT(varchar(20), @input)
/*
select result = @result
*/
return @result
end