• Twitter
  • Facebook
  • Google+
  • Instagram
  • Youtube

Tuesday, November 18, 2008

PadLeft and PadRight SQL Functions

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 !!


2 comments:

Raghavi Modi said...

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

オテモヤン said...

オナニー
逆援助
SEX
フェラチオ
ソープ
逆援助
出張ホスト
手コキ
おっぱい
フェラチオ
中出し
セックス
デリヘル
包茎
逆援
性欲

Contact

Get in touch with me


Adress/Street

12 Street West Victoria 1234 Australia

Phone number

+(12) 3456 789

Website

www.johnsmith.com