Solved

Format SQL 2005 View - add 6 zeros in front

Posted on 2014-10-16
2
146 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
[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
2 Comments
 
LVL 66

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

717 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