Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Simple Question...

Author  Topic 

mdelgado
Posting Yak Master

141 Posts

Posted - 2002-07-23 : 15:22:18
For some reason, I'm having trouble figuring this out although I'm sure the answer is simple.

I have a varchar column with the following values:

823
724
1224

How can I display them like this in a query?

0823
0724
1224

In other words; make the column 4 wided padded left with zeroes.

thanks.

joshb
Yak Posting Veteran

52 Posts

Posted - 2002-07-23 : 15:41:35
Here's one way:

SELECT REPLICATE('0', 4-LEN(Val)) + Val

Josh

Go to Top of Page

mdelgado
Posting Yak Master

141 Posts

Posted - 2002-07-23 : 15:50:58
EXCELLENT!!

thank you.

Go to Top of Page

drymchaser
Aged Yak Warrior

552 Posts

Posted - 2002-07-23 : 16:25:47
Another:

select right(('0000'+<fieldname>), 4)

Go to Top of Page

mdelgado
Posting Yak Master

141 Posts

Posted - 2002-07-23 : 16:31:56
Even Better Thanks.

Go to Top of Page
   

- Advertisement -