Solved

SQL Query - Append ZERO's in front of column with keeping equal field characters

Posted on 2013-01-21
4
545 Views
Last Modified: 2013-01-21
My dearest experts,

I have a table in which I have an Account Number ranging anywhere from 3 to 16 digits.  I'm using this table as part of a VIEW to extract data to push into another program.  I need to append "0"'s (zero's) to the front of all of the account numbers that are not 16 characters in length.

Example:   554433 needs to be 0000000000554433
Example:   87651234 needs to be 0000000087651234

Any and all assistance would be greatly appreciated.  

-Nick
0
Comment
Question by:NCollinsBBP
4 Comments
 
LVL 8

Accepted Solution

by:
virtuadept earned 500 total points
ID: 38802637
SELECT REPLACE(STR(CONVERT(FLOAT,account_number),16,0),' ','0')

converts the account number to a float, then to a 16 precision string with leading spaces, and finally replaces the spaces with zeros.
0
 
LVL 35

Expert Comment

by:David Todd
ID: 38802879
Hi,

select right( '0000000000000000' + convert( varchar( 16 ), account_number), 16 )

Converts account_number to varchar( 16 ), prepends leading zeros, and takes the right-most 16 characters.

HTH
  David
0
 

Author Closing Comment

by:NCollinsBBP
ID: 38802890
This did exactly what I needed it to do.  Thank you, virtuadept!
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38802913
I assume the results will have to be in character format to keep the leading zeros but this is a common way to to that with varying lengths for input.
Prepend the existing value with more than enough zeros and then take the RIGHT 16 of them for your result.

SELECT RIGHT(REPLICATE('0',16)+AN.Account_Number,16) Padded_AccountNumber
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

24 Experts available now in Live!

Get 1:1 Help Now