PadLeft Function
=================
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
PadRight Function
=====================
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
Hope you will like it !!
1 comments:
Hi Abhishek,
Better option to do padding is using replicate function rather than having while loop.
you can use [your text] + replicate([padding text] , total length - length of your text])
i.e. select 'text' + replicate('*', 10 - len('text'))
-Ragahvi
Post a Comment