amma.NETtami

.NET walkabout
posts - 12, comments - 25, trackbacks - 0

My Links

News

giossi.com - Two steps ahead

↑ Grab this Headline Animator

Tag Cloud

Archives

Post Categories

Code repository websites

Websites

How to format a value using T-SQL: pad left

SQL server has no built-in format functions. Here is a user defined function which left-pad a varchar value with a variable length string.

CREATE FUNCTION [dbo].[PadString]

(@Seq varchar(16),
@PadWith char(1),
@PadLength int
)

RETURNS varchar(16) AS

BEGIN

declare @curSeq varchar(16)

SELECT @curSeq = ISNULL(REPLICATE(@PadWith, @PadLength - len(ISNULL(@Seq ,0))), '') + @Seq

RETURN @curSeq

END

Testing the function:

SELECT dbo.PadString ('8', '0', 5)

SELECT dbo.PadString ('abc', '*', 12)

SELECT dbo.PadString ('abc', '0', 7)

 

Here are the results:

                
----------------
00008

(1 row(s) affected)

                
----------------
*********abc

(1 row(s) affected)

                
----------------
0000abc

(1 row(s) affected)

---------------------------------------------------------

TAGS: sql server format string pad left

Print | posted on giovedì 15 novembre 2007 8.55 | Filed Under [ T-SQL ]

Feedback

Gravatar

# re: How to format a value using T-SQL: pad left

OR...

SELECT REPLACE(STR(@mynumber, @padding), ' ', '0')
17/06/2008 17.55 | Anton Swanevelder
Gravatar

# re: How to format a value using T-SQL: pad left

Anton,
I have no sql query analyzer with me right now, but if I'm not wrong, the function STR works with numbers.

SELECT REPLACE(STR(4, 5), ' ', '0')

generates an output like this

00004

SELECT REPLACE(STR(4, '@'), ' ', '0')

... will not work

Am I wrong?
I don't get what you mean :s
18/06/2008 3.55 | Francesco Giossi
Gravatar

# re: How to format a value using T-SQL: pad left

Nice work!!!
It works fine and it is very useful
tkx.
16/07/2008 21.15 | JiPe
Gravatar

# re: How to format a value using T-SQL: pad left

Immenso joss, viene in aiuto agli ex colleghi attraverso google.

Omnipresente.
16/12/2008 14.56 | jop
Gravatar

# re: How to format a value using T-SQL: pad left

good work,
27/03/2009 5.48 | kkun
Gravatar

# re: How to format a value using T-SQL: pad left

thankss very nice...
16/07/2009 22.27 | çet
Gravatar

# re: How to format a value using T-SQL: pad left

Display numbers in fixed format. Like

1 as 0000001
109 as 0000109
0987 as 0000987

I have use AdventureWorks database for our example purpose. I have noticed that some developers does not use CAST function but I strongly suggest to use it as if CAST function is not used and your column is of numeric datatype it will give errors.


/* Method 1 Using RIGHT function*/
SELECT TOP 10 RIGHT('0000000'
+ CAST(ContactID AS VARCHAR(7)), 7) PaddedContactID
FROM Person.Contact

/* Method 2 Using RIGHT AND REPLICATE function*/
SELECT TOP 10 RIGHT(REPLICATE('0', 7)
+ CAST(ContactID AS VARCHAR(7)), 7) PaddedContactID
FROM Person.Contact
22/07/2009 7.56 | Hafeez Ansari
Gravatar

# re: How to format a value using T-SQL: pad left

@Hafeez Ansari
I agree with you, I should use CAST function with this function.
Anyway, the function will not generate an error if the column is of numeric datatype. Check it! ;)
22/07/2009 13.01 | Francesco Giossi
Gravatar

# re: How to format a value using T-SQL: pad left

Great Post! Very useful information is given. Thanks.
Gravatar

# re: How to format a value using T-SQL: pad left

excellent!
this tip is what i wished!
28/01/2010 3.50 | Hiro

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 7 and type the answer here:

Powered by: