Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Integer to Have Preceeding 0's

Status
Not open for further replies.

AccessUser22

Technical User
Joined
Jan 23, 2003
Messages
168
Location
US
If I have an integer being passed in, say 2 for example, and I want to store this in part of a string as 002, is there a way to set this up during my stored procedure using the CAST function? Any ideas are appreciated.
 
Did you what something like this?

Code:
declare @num int

select @num = 1

select '00'+convert(varchar(100),@num)

- Paul
- If at first you don't succeed, find out if the loser gets anything.
 
convert(varchar(3), REPLICATE('0', 3 - LEN(@num))) + convert(varchar(3), @num)
 
SELECT RIGHT('000' + CONVERT(CHAR(3), @num), 3)

<.

 
I guess the question should be.. Do you always want to pad all your ints with leading 'O's. like
001
0010
00100

Or are you looking for something more like this.

001
010
100




- Paul
- If at first you don't succeed, find out if the loser gets anything.
 
monksnake, if you want right results you must CAST to varchar not to CHAR()
Code:
SELECT RIGHT('000' + CONVERT(varchar(3), @num), 3)

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
AAAAHHHh ok, thanks for the info bborissov

<.

 
I'm looking for something more like

001
010
100

I'm gonna give the above example

SELECT RIGHT('000' + CONVERT(varchar(3), @num), 3)

a try. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top