mo.notono.us

Monday, August 15, 2005

PadLeft and PadRight SQL Functions

--Left-pads a string with a specified character to achieve a desired length
--Created 08/15/05 by Oskar Austegard
CREATE FUNCTION dbo.PadLeft(
 @String varchar(100), --Input string to be padded
 @Length int,          --Length of final string
 @PadChar char(1)      --Padding character
)
RETURNS varchar(100)
AS
BEGIN
 WHILE LEN(@String + 'z') <= @Length
  SET @String = @PadChar + @String
 RETURN @String
END

GO

--Right-pads a string with a specified character to achieve a desired length
--Created 08/15/05 by Oskar Austegard
CREATE FUNCTION dbo.PadRight(
 @String varchar(100), --Input string to be padded
 @Length int,          --Length of final string
 @PadChar char(1)      --Padding character
)
RETURNS varchar(100)
AS
BEGIN
 WHILE LEN(@String + 'z') <= @Length
  SET @String = @String + @PadChar
 RETURN @String
END

Labels:

0 Comments:

Post a Comment

<< Home