Solved

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

Posted on 2013-06-26
6
465 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
  • 4
  • 2
6 Comments
 
LVL 65

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 65

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 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 65

Expert Comment

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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now