String and Numeric PaddingHave you ever ran into one of those cases when you needed to fix the length of a value? Well, it turns out to be a pretty straight forward task. These functions can be easily adapted to pad some other value as well.
CREATE FUNCTION PadNumeric
(
@Numeric Numeric,
@PadTo Int = 10
)
RETURNS VarChar(50)
AS
BEGIN
RETURN Replace(Space(@PadTo - Len(Convert(Varchar, @Numeric))), ' ', 0) + Convert(Varchar, @Numeric)
END
CREATE FUNCTION [dbo].[PadLeft]
(
@String VarChar(50),
@PadTo Int = 10
)
RETURNS VarChar(50)
AS
BEGIN
RETURN Replace(Space(@PadTo - Len(@String)), ' ', 0) + @String
END
CREATE FUNCTION [dbo].[PadRight]
(
@String VarChar(50),
@PadTo Int = 10
)
RETURNS VarChar(50)
AS
BEGIN
RETURN @String + Replace(Space(@PadTo - Len(@String)), ' ', 0)
END
Is this code snippet, product or advice warrantied against ill-effect and/or technical malaise? No. No it's not! Not expressed - Not implied - not at all.