Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Modifying a SQL Store Procedure

Posted on 2014-03-19
3
Medium Priority
?
509 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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 66

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

885 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