Solved

Modifying a SQL Store Procedure

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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…
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.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

820 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