Solved

Modifying a SQL Store Procedure

Posted on 2014-03-19
3
419 Views
Last Modified: 2014-03-19
I'm trying to modify a store procedure in SQL 2005 and for the life of me can not figure it out. I was able to edit other parts of the script. I am trying to Zero out the highlighted (1471961) text. Some times that number can grow or shrink.

 Example of what Im trying to Zero out.

this store procedure is this

--20.                 Build & add File Control Record into final table
INSERT INTO        tblACH ( LINE, ORDERNO, REC )
SELECT             '9000001'
                    + LEFT('000000',6 - LEN(CAST(([3EC] + 4)/10 AS int)
                    + CASE WHEN ([3EC] + 4 - (10*(([3EC] + 4)/10))) > 0 THEN 1 ELSE 0 END))
                    + CAST(CAST(([3EC] + 4)/10 AS int)
                    + CASE WHEN ([3EC] + 4 - (10*(([3EC] + 4)/10))) > 0 THEN 1 ELSE 0 END AS varchar(200))
                    + RIGHT('00000000'+ CAST([3EC] AS varchar(200)),8)
                    + RIGHT('0000000000'+ CAST([4EH] AS varchar(200)),10)
                    + RIGHT('000000000000'+CAST(CAST([56TDA] AS decimal(20,0)) AS varchar(200)),12)
                    + RIGHT('000000000000'+CAST(CAST([56TDA] AS decimal(20,0)) AS varchar(200)),12)
                    + '                                       ',
                   999,
                   1
FROM               (SELECT  '8200' AS [12],
                            COUNT(tblACH_DA_Temp.REC) AS [3EC],
                            SUM(CAST (tblACH_DA_Temp.BC4 AS int)) AS [4EH],
                            SUM([BC5])*100 AS [56TDA]
                    FROM    tblACH_DA_Temp) AS INNERQ3

END
0
Comment
Question by:AccordJ35
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39939537
you mean, put 000000000000000 instead, in that INSERT statement?
in that case, you only have to replace the relevant field by "0":
INSERT INTO        tblACH ( LINE, ORDERNO, REC )
SELECT             '9000001'
                    + LEFT('000000',6 - LEN(CAST(([3EC] + 4)/10 AS int)
                    + CASE WHEN ([3EC] + 4 - (10*(([3EC] + 4)/10))) > 0 THEN 1 ELSE 0 END))
                    + CAST(CAST(([3EC] + 4)/10 AS int)
                    + CASE WHEN ([3EC] + 4 - (10*(([3EC] + 4)/10))) > 0 THEN 1 ELSE 0 END AS varchar(200))
                    + RIGHT('00000000'+ CAST([3EC] AS varchar(200)),8)
                    + RIGHT('0000000000'+ CAST([4EH] AS varchar(200)),10)
                    + '000000000000'
                    + RIGHT('000000000000'+CAST(CAST([56TDA] AS decimal(20,0)) AS varchar(200)),12)
                    + '                                       ',
                   999,
                   1
FROM               (SELECT  '8200' AS [12],
                            COUNT(tblACH_DA_Temp.REC) AS [3EC],
                            SUM(CAST (tblACH_DA_Temp.BC4 AS int)) AS [4EH],
                            SUM([BC5])*100 AS [56TDA]
                    FROM    tblACH_DA_Temp) AS INNERQ3

END 

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39939558
>I am trying to Zero out the highlighted (1471961) text.
If by 'zero out' you mean 'delete it and return the rest of the string, and the number is always 1471961, then use the REPLACE function.
Declare @str varchar(100) = '9000001000001000000060042427212000001471961000001471961'

SELECT REPLACE(@str, '1471961', '')

Open in new window

BUT, it looks like you have two instances of '1471961', so to delete only the first one, use the CHARINDEX function to find the first instance of 1471961.

Declare @str varchar(100) = '9000001000001000000060042427212000001471961000001471961'

/*
-- Troubleshooting
SELECT REPLACE(@str, '1471961', '')
SELECT LEN(@str)
SELECT CHARINDEX('1471961', @str)
*/

SELECT LEFT(@str, CHARINDEX('1471961', @str) - 1)  + RIGHT(@str, LEN(@str) - CHARINDEX('1471961', @str) - 7)

Open in new window

0
 

Author Closing Comment

by:AccordJ35
ID: 39939620
Thanks this solved it! I needed the string to return all zeros but keep the length as 12 zeros. I was staring at this code for a while and came to the same solution. I came back to update and saw this was the same. lol. Ah feels good to learn haha.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

743 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