Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Zero filling zip code in SQL and converting from character to numeric

Posted on 2013-06-26
6
Medium Priority
?
505 Views
Last Modified: 2013-06-26
Experts,

I am coverting a  column from character to numeric in SQL.  I would like to see the "0" at the leading zeroes.  Can someone tell me how to format the output?

This is what I have to convert from character to numeric.


,(select convert( numeric( 5, 0 ), left( member.member_zip, 5 ))) as zip
0
Comment
Question by:morinia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39279130
>I am coverting a  column from character to numeric in SQL.  
>I would like to see the "0" at the leading zeroes.
What you are asking is not possible, as no numeric data types support leading zeros.
So ... it can't be done.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39279134
To 'pad' a number with leading zeros and display it..

Declare @num int = 1234

SELECT RIGHT('00000' + CAST(@num as varchar(max)), 5)

Open in new window

0
 

Author Comment

by:morinia
ID: 39279199
jihorn,

This gives me leading zeroes, but it is  1024 characters.  I want 5 positions numeric.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 66

Accepted Solution

by:
Jim Horn earned 1000 total points
ID: 39279231
>I want 5 positions numeric.
You can have this as 5 positions varchar, but as I stated above numeric is not possible.   No way, no how.

Declare @num int = 1234

SELECT CAST(RIGHT('00000' + CAST(@num as varchar(max)), 5) as varchar(5))

Open in new window

0
 

Author Closing Comment

by:morinia
ID: 39279269
Thanks
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39279277
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question