Technical Article

LPAD Function

LPAD returns char1, left-padded to length n with the sequence of characters in char2; <BR>char2 defaults to a single blank. If char1 is longer than n, this function returns the portion of char1 that fits in n. The argument n is the total length of the return value as it is displayed on your terminal screen. In most character sets, this is also the number of characters in the return value.

CREATE FUNCTION LPAD(
@Char1  VARCHAR(8000), 
@n INT, 
@Char2  VARCHAR(255)) 
RETURNS VARCHAR(8000)
AS
BEGIN
IF LEN(@Char1) > @n
BEGIN
IF SIGN(@n) = -1
    RETURN SUBSTRING(@Char1, 1, 0)
RETURN SUBSTRING(@Char1, 1, @n)
END
RETURN REPLICATE(@Char2, @n - LEN(@Char1)) + @Char1
END

Rate

5 (3)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (3)

You rated this post out of 5. Change rating