all groups > sql server programming > march 2004 >
You're in the

sql server programming

group:

oadl (or padr)



Re: oadl (or padr) Adam Machanic
3/23/2004 9:57:25 AM
sql server programming: SELECT REPLICATE('0', n - LEN(MyCol)) + MyCol

Where MyCol is assumed to be a character type and n is the number of total
characters you want the final output to be. Also, this assumes that for
every case, LEN(MyCol) <= n.


[quoted text, click to view]

Re: oadl (or padr) Itzik Ben-Gan
3/23/2004 5:03:28 PM
RIGHT(REPLICATE('0', num_digits-1) + CAST(number AS VARCHAR(num_digits)),
num_digits)

e.g., @i is an INT, with a max number of 10 digits:

RIGHT(REPLICATE('0', num_digits-1) + CAST(number AS VARCHAR(num_digits)),
num_digits)

DECLARE @n AS INT
SET @n = 1
SELECT RIGHT(REPLICATE('0', 9) + CAST(@n AS VARCHAR(10)), 10)

--
BG, SQL Server MVP
www.SolidQualityLearning.com


[quoted text, click to view]

oadl (or padr) toylet
3/23/2004 10:53:10 PM
Does SQL server has functions to pad a string with leading zero?

"1" --> "001"

I wrote my own, but is looking for some native features.

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
AddThis Social Bookmark Button