By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,856 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,856 IT Pros & Developers. It's quick & easy.

converting integer zipcodes to a 5 character char

P: n/a
hello all,

is there a quick way to convert a zipcode of type int, to a 5 character
char value ?
e.g.

declare @zip int
declare @czip char(5)
select @zip = 2109
select @czip = convert(char, @zip)
select @czip

but with @czip = 02109 instead of 2109 ?
thanks in advance

May 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
SQL
Here is one way
declare @zip int
declare @czip char(5)
select @zip = 2109
select @czip = convert(char, RIGHT('00000' + CONVERT(VARCHAR,@zip),5))
select @czip

Denis the SQL Menace
http://sqlservercode.blogspot.com/

May 23 '06 #2

P: n/a
select substring(convert(char,100000+123),2,5)
select substring(convert(char,100000+1234),2,5)
select substring(convert(char,100000+12345),2,5)
-----
00123

(1 row(s) affected)
-----
01234

(1 row(s) affected)
-----
12345

(1 row(s) affected)

May 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.