?
Solved

How do I Zero Fill a SQL Query Result

Posted on 2012-03-14
10
Medium Priority
?
1,034 Views
Last Modified: 2012-03-14
We are needing a Query to pull information from our Database so we can submit the information in a Text format to the Bank.

I've got most of it figured out, but the one part I can't figure out is, where the Check Number is say 5 digits long the result needs to Zero Fill the remaining with Zeros to fill it to the right for 10 Digits.

SELECT '000123456789001'+LTRIM(CAST(CHECKNO AS VARCHAR(20)))+ CONVERT(varchar(10),CHECKDT, 112)+ REPLACE (CAST(CHECKNET AS VARCHAR(20)), '.',  '')+CASE(CKSTATUS)
WHEN 'O' THEN '' WHEN 'V' THEN 'C' END +'' as Val
FROM dbo.CHECK
WHERE      CHECKDT BETWEEN '02/28/12' and '02/28/12' and bankid='MYBANK' and ISNUMERIC(CHECKNO)=1


The above Query gives me the Following Result

00012345678900129974201202281234569

Just after the 9001 you will see a 29974. That's my Check Number, I need Zeros added to the Left of that number based off how many digits the Check number ended up being when the Query was ran.

Is there a way to do this?
0
Comment
Question by:Rmumpower00
[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
  • 4
  • 2
10 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 1000 total points
ID: 37720396
http://sqlusa.com/bestpractices2005/padleadingzeros/

-- Add/pad leading zeros to numeric string for a total length of 12 - QUICK SYNTAX

DECLARE @Amount varchar(32) = '78912'

SELECT STUFF(@Amount, 1, 0, REPLICATE('0', 12 - LEN(@Amount))) -- 000000078912
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 1000 total points
ID: 37720407
Hi Slightly easier approach might be to use the RIGHT function.


SELECT right ('000000000000' + your_value, 12) FROM xxxx


Just make sure that the string of zeroes is as long as your desired output.



Good Luck,
Kent
0
 

Author Comment

by:Rmumpower00
ID: 37720427
The String of Zeros would change though as the Check Numbers grew, is there a way it will read how many digits the check number was and add the appropriate Zeros?
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 37720455
It doesn't matter.  

Either solution above will have the length of the field hard-coded into the SQL.   REPLICATE (12 - LEN..., or RIGHT ( ..., 12)

As long as you REPLICATE enough digits, or have them hard coded into the concatenated string, you're fine.  The value '12' is the critical component.

And you can mix these two solutions.  Either can use REPLICATE or a hard coded string.  The real difference is calling LEN or RIGHT.


Good Luck,
Kent
0
 

Author Comment

by:Rmumpower00
ID: 37720494
I'm trying to place what you guys posted into the Query and I'm getting errors. Could you put the String in the Query that I posted to make it a bit easier on me?

Thank you
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 37720525
It's always helpful to know what errors you get.  :)  Sometimes a very simple change to your SQL is the easiest path.

SELECT
  '000123456789001'+
  RIGHT ('000000000000' + LTRIM(CAST(CHECKNO AS VARCHAR(20))), 12) +
  CONVERT(varchar(10),CHECKDT, 112)+
  REPLACE (CAST(CHECKNET AS VARCHAR(20)), '.',  '')+
  CASE(CKSTATUS)
  WHEN 'O' THEN '' WHEN 'V' THEN 'C' END +'' as Val
  FROM dbo.CHECK
  WHERE      CHECKDT BETWEEN '02/28/12' and '02/28/12'
    and bankid='MYBANK' and   ISNUMERIC(CHECKNO)=1

Try that.  :)

Kent
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37720536
You can set the number based on a variable though:

SELECT STUFF(@Amount, 1, 0, REPLICATE('0', 12 - LEN(@Amount))) -- 000000078912

becomes:

declare @total_digits
set @total_digits = 12  -- or select from an options table)

SELECT STUFF(@Amount, 1, 0, REPLICATE('0', @total_digits  - LEN(@Amount))) -- 000000078912
0
 

Author Comment

by:Rmumpower00
ID: 37720556
Ged325, What you wrote looks more like what I'm trying to achieve here. Could you place it in the Query I currently have so I can just copy / paste it?

@ Kent, That query worked great! IF I can't get this to automatically determine how many digits the Check Number is and automatically fill it with the appropriate Zero's then I'll use your query and just go with Hard Coding it in.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 37720583
You don't need to know how many digits the check number is.  Whether it's 1, 4, 12 or anything else, either of those solutions will pad it with enough leading zeros to get you a 12 character string.


Kent
0
 

Author Comment

by:Rmumpower00
ID: 37720665
Outstanding! That worked perfectly. Now, the only one left is the REPLACE (CAST(CHECKNET AS VARCHAR(20)), '.',  '') line.

How do I put the Zeros on this to equal 10?
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
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…

762 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