Solved

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

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

16 Experts available now in Live!

Get 1:1 Help Now