Solved

Modifying a SQL Store Procedure

Posted on 2014-03-19
3
484 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

617 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