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


Padding function


Padding function

Author
Message
Site Owners
Site Owners
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: Administrators
Points: 18685 Visits: 225
Comments posted to this topic are about the item Padding function
Toby Harman
Toby Harman
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2379 Visits: 673
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
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 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 Cutshall
Aaron Cutshall
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4874 Visits: 1211
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
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 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
Say Hey Kid
Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)

Group: General Forum Members
Points: 678 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
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 29
My guess is that the original function is going to have serious scalability issues.
Ian Crandell
Ian Crandell
SSC Eights!
SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)

Group: General Forum Members
Points: 841 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
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38996 Visits: 886
Thanks for the script.
Iwas Bornready
Iwas Bornready
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38996 Visits: 886
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