I'm trying to replace some input strings in a formula to zero value. I was able to do it for certain strings but wasn't able to do for all the strings listed in the below #mathtemp table.
I have the input names, I have the formula and all I need to do is just repalce the strings which doesn't have numbers(values) to zero. There is some mistake in the replace statement which I am not able to figure out.
I tried using ISNUMERIC to get this done but that doesn't give the required output. Let me know if there is any new idea to do this or to solve the below one.
BEGIN
DECLARE @INPUTCOUNT AS INTEGER
DECLARE @formulacount as integer
DECLARE @in as int
DECLARE @ia as int
DECLARE @OUTPUTFORMULATrade as nvarchar(160)
set @ia = 1
set @in = 1
CREATE TABLE #STATICFILTER
(
IDNUM INTEGER IDENTITY(1,1),
STATICNAME Varchar(160),
)
INSERT INTO #STATICFILTER (STATICNAME)
VALUES ('Import - Consumption customs value(2266)') ,('Import - Consumption customs value(1540)') ,('Import - Consumption customs value(1541)')
SET @INPUTCOUNT = (select count(*) from #STATICFILTER)
CREATE TABLE #MathTemp1
(
IDNUM INTEGER IDENTITY(1,1),
YEARMONTH VARCHAR(256),
OUTPUTFORMULA VARCHAR(256),
Timedimensiondate Date
)
INSERT INTO #MathTemp1 (YEARMONTH,OUTPUTFORMULA,Timedimensiondate)
VALUES ('CV(N2) 1989: 1','2641.000 + Import - Consumption customs value(1540) + Import - Consumption customs value(1541)','1989-01-01')
,('CV(N2) 1989: 10','54407.000 + Import - Consumption customs value(1540) + 63906.000','1989-10-01')
,('CV(N2) 1990: 11','Import - Consumption customs value(2266) + Import - Consumption customs value(1540) + 53088.000','1990-11-01')
,('CV(N2) 1994: 5','32852.000 + Import - Consumption customs value(1540) + Import - Consumption customs value(1541)','1994-05-01')
SET @formulacount = (select count(*) from #MathTemp1)
while (@ia < @formulacount)
begin
WHILE (@in < @INPUTCOUNT)
BEGIN
SET @OUTPUTFORMULATrade = (Select Replace ((SELECT REPLACE(OUTPUTFORMULA,(select STATICNAME from #STATICFILTER where IDNUM = @in),0)FROM #MathTemp1 WHERE IDNUM = @ia),(select STATICNAME from #STATICFILTER where IDNUM = @in+1),0))
SET @in = @in + 1
END
SET @ia = @ia + 1
SET @in =1
Select @OUTPUTFORMULATrade AS New
END
drop table #MathTemp1
drop table #STATICFILTER
END
please note the inputs are arranged in a random format in #staticfilter table and similarly #mathtemp table might also have any combination of input formulas.
My desired output is to replace the strings in any given formula to zero vlaues. please check below
2641.000 + 0 + 0
54407.000 + 0 + 63906.000
0 + 0 + 53088.000