zero padding

From: Pranay Pandya (ppandya_at_orthodon.com)
Date: 10/13/04


Date: 13 Oct 2004 13:11:28 -0700

Below is a function i created for adding zero padding. The function
works if i want to add '0' padding at the back.
for example test00000.
But if i change the function to make it pad '0's in the front it does
not work.
for example 00000test DOES NOT work. The commented code in the
function below does not work.
CREATE FUNCTION udf_ZeroPadding(@VarcharIn char(50), @RequiredLength
int)
RETURNS varchar(50)
AS BEGIN
        DECLARE @VarcharOut char(50)
        DECLARE @CurrentLength int
        SET @CurrentLength = LEN(@VarcharIn)
        select @VarcharOut = @VarcharIn
        WHILE @CurrentLength < @RequiredLength
        BEGIN
                --SET @VarcharOut = '0' + @VarcharOut
                --The above line does not work.
                SET @CurrentLength = LEN(@VarcharOut)
        END
        RETURN LTRIM(RTRIM(@VarcharOut))
END

Thank You,
-Pranay


Loading