在sql server中实现PadLeft函数

create function PadLeft(@num varchar(16),@paddingChar char(1),@totalWidth int)
returns varchar(16) as
begin
declare @curStr varchar(16)
select @curStr = isnull(replicate(@paddingChar,@totalWidth - len(isnull(@num ,0))), '') + @num
return @curStr
end


测试:

SELECT dbo.PadLeft('8', '0', 6)

----------------
000008

(所影响的行数为 1 行)


你可能感兴趣的:(在sql server中实现PadLeft函数)