Solved

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

Posted on 2013-01-21
4
551 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
[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 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 27

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article I will describe the Backup & Restore 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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

626 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