﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Francis Jamet  / Padding function / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 07:17:55 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Padding function</title><link>http://www.sqlservercentral.com/Forums/Topic466454-1219-1.aspx</link><description>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.</description><pubDate>Tue, 29 Apr 2008 11:30:51 GMT</pubDate><dc:creator>Ian Crandell</dc:creator></item><item><title>RE: Padding function</title><link>http://www.sqlservercentral.com/Forums/Topic466454-1219-1.aspx</link><description>My guess is that the original function is going to have serious scalability issues.</description><pubDate>Mon, 21 Apr 2008 11:51:22 GMT</pubDate><dc:creator>jamie.lamorgese</dc:creator></item><item><title>RE: Padding function</title><link>http://www.sqlservercentral.com/Forums/Topic466454-1219-1.aspx</link><description>:smooooth:  Yet another padding function, pure genius at least in my opinion  LOL  :)[code]/* ************************************************************** 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 &amp;lt; 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 ''	   ENDEND[/code]</description><pubDate>Mon, 21 Apr 2008 11:05:49 GMT</pubDate><dc:creator>Mike Garrison</dc:creator></item><item><title>RE: Padding function</title><link>http://www.sqlservercentral.com/Forums/Topic466454-1219-1.aspx</link><description>Yet another padding function. This will let you use strings instead of a single character.[code]/*Example:						Result:SELECT dbo.fn_Padding('1', '0', 3, 'L')			001SELECT dbo.fn_Padding('15', '0', 3, 'L')			015SELECT 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[/code]</description><pubDate>Mon, 21 Apr 2008 09:30:57 GMT</pubDate><dc:creator>tim.luypaert-680263</dc:creator></item><item><title>RE: Padding function</title><link>http://www.sqlservercentral.com/Forums/Topic466454-1219-1.aspx</link><description>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.[code]-- =============================================-- 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)ASBEGIN	RETURN CASE WHEN @Orientation = 'R' THEN @OrigStr ELSE '' END +				REPLICATE(@PadChar, @Length - LEN(@OrigStr)) +				CASE WHEN @Orientation = 'L' THEN @OrigStr ELSE '' ENDEND[/code]</description><pubDate>Mon, 21 Apr 2008 06:38:09 GMT</pubDate><dc:creator>Aaron N. Cutshall</dc:creator></item><item><title>RE: Padding function</title><link>http://www.sqlservercentral.com/Forums/Topic466454-1219-1.aspx</link><description>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.</description><pubDate>Mon, 21 Apr 2008 06:03:39 GMT</pubDate><dc:creator>Todd Sherman-487999</dc:creator></item><item><title>RE: Padding function</title><link>http://www.sqlservercentral.com/Forums/Topic466454-1219-1.aspx</link><description>I marginally prefer my version... Its a little shorterNeeds comments I know, but the parameters are exactly the same[code]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[/code]</description><pubDate>Sun, 20 Apr 2008 22:16:38 GMT</pubDate><dc:creator>Toby Harman</dc:creator></item><item><title>Padding function</title><link>http://www.sqlservercentral.com/Forums/Topic466454-1219-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/String+Manipulation/62479/"&gt;Padding function&lt;/A&gt;[/B]</description><pubDate>Sun, 09 Mar 2008 19:57:58 GMT</pubDate><dc:creator>Site Owners</dc:creator></item></channel></rss>