Solved

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

Posted on 2013-01-21
4
547 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

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.

Question has a verified solution.

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

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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