Solved

Format SQL 2005 View - add 6 zeros in front

Posted on 2014-10-16
2
144 Views
Last Modified: 2014-10-16
I have a SQL 2005 server and I have value that is a number. The number can be 1 or 2 or 3 digits maximum BUT I need the number to be 8 digits leading zeros for an export system.

Example:  
My numbers:
3
12
120

What I need in my sql view:  (notice it is 8 characters with leading zeros)
00000003
00000012
00000120

Here is my basic statement:
Select Invoices.InvoiceQuantity
FROM dbo.Invoices
0
Comment
Question by:allenkent
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40385324
No can do as a numeric data type, as they do not support leading zeros.

The only way to pull that off is to convert it to a char with leading zeros, and use RIGHT to handle the number of zeros correctly, in the set that displays it.
For a demo, copy-paste and execute the below into your SSMS
CREATE TABLE #tmp (numbers int) 

INSERT INTO #tmp (numbers) 
VALUES (3), (12), (120) 

SELECT RIGHT('00000000' + CAST(numbers as varchar(8)), 8) as my_number 
FROM #tmp

Open in new window

0
 

Author Closing Comment

by:allenkent
ID: 40385351
PERFECT!
SELECT RIGHT('00000000' + CAST(numbers as varchar(8)), 8) as my_number
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 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