Solved

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

Posted on 2013-06-26
6
463 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
Comment Utility
>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
Comment Utility
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
Comment Utility
jihorn,

This gives me leading zeroes, but it is  1024 characters.  I want 5 positions numeric.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
Comment Utility
>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
Comment Utility
Thanks
0
 
LVL 65

Expert Comment

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

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

743 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

17 Experts available now in Live!

Get 1:1 Help Now