Solved

Modifying a SQL Store Procedure

Posted on 2014-03-19
3
470 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
[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
3 Comments
 
LVL 143

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

739 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