SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Padding function


Padding function

Author
Message
Toby Harman
Toby Harman
SSC Eights!
SSC Eights! (807 reputation)SSC Eights! (807 reputation)SSC Eights! (807 reputation)SSC Eights! (807 reputation)SSC Eights! (807 reputation)SSC Eights! (807 reputation)SSC Eights! (807 reputation)SSC Eights! (807 reputation)

Group: General Forum Members
Points: 807 Visits: 668
I marginally prefer my version... Its a little shorter

Needs comments I know, but the parameters are exactly the same


CREATE FUNCTION fn_MyPadding (
@Text AS VARCHAR(500),
@Char AS CHAR(1),
@HowMany AS INT,
@LeftRight AS CHAR(1) = 'L')
RETURNS VARCHAR(500) AS
BEGIN
IF @Text IS NULL
SET @Text = @Char

DECLARE @Count SMALLINT
SET @Count = LEN(@Text)

IF @LeftRight = 'L'
SET @Text = REVERSE(@Text)

SET @Text = @Text + REPLICATE(@Char, @HowMany - @Count)

IF @LeftRight = 'L'
SET @Text = REVERSE(@Text)

RETURN (@Text)
END


Todd Sherman-487999
Todd Sherman-487999
Valued Member
Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)

Group: General Forum Members
Points: 71 Visits: 148
It should be fairly easy to also have a center function. Just the same as this function called twice. In the first call, do the length of the string as int(length/2) as a left pad. In the second call, do round(length/2, 0) as a right pad.

That way, you can have text centered for a header block or to fill out a check fancy or something.
Aaron N. Cutshall
Aaron N. Cutshall
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2111 Visits: 1043
This is a variant of the version that I use. I've modified it to use the same arguments as the one given in the article. I liked the comment given about a center orientation. Perhaps I'll modify this to include that feature as well.


-- =============================================
-- Author: Aaron N. Cutshall
-- Create date: 21-Apr-2008
-- Description: Pad string with given character for length given
-- =============================================
ALTER FUNCTION fn_PadStr(
@OrigStr varchar(512),
@PadChar char(1) = ' ',
@Length int,
@Orientation char(1) = 'L')
RETURNS varchar(512)
AS
BEGIN
RETURN CASE WHEN @Orientation = 'R' THEN @OrigStr ELSE '' END +
REPLICATE(@PadChar, @Length - LEN(@OrigStr)) +
CASE WHEN @Orientation = 'L' THEN @OrigStr ELSE '' END
END




"...when ye are in the service of your fellow beings ye are only in the service of your God." -- Mosiah 2:17
tim.luypaert-680263
tim.luypaert-680263
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 10
Yet another padding function. This will let you use strings instead of a single character.


/*
Example: Result:
SELECT dbo.fn_Padding('1', '0', 3, 'L') 001
SELECT dbo.fn_Padding('15', '0', 3, 'L') 015
SELECT dbo.fn_Padding('ABC', '*', 10, 'R') ABC*******
SELECT dbo.fn_Padding('ABC', '_°', 10, 'R') ABC_°_°_°_
SELECT dbo.fn_Padding('ABC', '_°', 10, 'L') °_°_°_°ABC
*/

CREATE FUNCTION dbo.fn_Padding
(
@Text VARCHAR(8000),
@PaddingChars VARCHAR(20),
@StringLength INT,
@Direction CHAR(1) = 'L'
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @PaddingString VARCHAR(8000)

-- Build string to use for padding
SET @PaddingString = REPLICATE(@PaddingChars, (@StringLength / LEN(@PaddingChars)) + 1)

-- Left padding
IF (@Direction = 'L')
SET @Text = RIGHT(@PaddingString + COALESCE(@Text, ''), @StringLength)

-- Right padding
IF (@Direction = 'R')
SET @Text = LEFT(COALESCE(@Text, '') + @PaddingString, @StringLength)

RETURN @Text
END


Mike Garrison
Mike Garrison
SSChasing Mays
SSChasing Mays (660 reputation)SSChasing Mays (660 reputation)SSChasing Mays (660 reputation)SSChasing Mays (660 reputation)SSChasing Mays (660 reputation)SSChasing Mays (660 reputation)SSChasing Mays (660 reputation)SSChasing Mays (660 reputation)

Group: General Forum Members
Points: 660 Visits: 42
Smooooth Yet another padding function, pure genius at least in my opinion LOL Smile

/* *************************************************************
* Author: Mike Garrison
* Create Date: 04-21-08
* Description: Pad a string to a given length to either side
**************************************************************** */

ALTER FUNCTION dbo.udf_PadString (
@Text VARCHAR(500) = '',
@Char VARCHAR(50),
@Length INT,
@Orientation CHAR(1) = 'L')

RETURNS VARCHAR(500)
AS
BEGIN
IF @Length < LEN (@Text)
SET @Length = LEN(@Text)
RETURN CASE WHEN @Text IS NULL THEN REPLICATE(@char, @length)
WHEN @Orientation = 'L' THEN RIGHT(REPLICATE(@char, @length) + @text, @Length)
WHEN @Orientation = 'R' THEN LEFT(@text + REPLICATE(@char, @length) , @Length)
ELSE ''
END
END



jamie.lamorgese
jamie.lamorgese
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 29
My guess is that the original function is going to have serious scalability issues.
Ian Crandell
Ian Crandell
Old Hand
Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)

Group: General Forum Members
Points: 363 Visits: 1093
Good article. I appreciate the way you put a parameter list at the top of the article as well as in the function itself. This made it easier to understand at first glance.

I also enjoyed the variations that others have provided.

Ian.

"If you are going through hell, keep going."
-- Winston Churchill
Iwas Bornready
Iwas Bornready
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16098 Visits: 885
Thanks for the script.
Iwas Bornready
Iwas Bornready
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16098 Visits: 885
Ian Crandell (4/29/2008)
Good article. I appreciate the way you put a parameter list at the top of the article as well as in the function itself. This made it easier to understand at first glance.

I also enjoyed the variations that others have provided.
Me too
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search