Excel to SQL Case - sql

I am trying to convert a really convoluted EXCEL sheet into SQL stored procedure. There is one last formula that is kicking my butt though:
=IF(AND (W4+H4<=0,IF(E4-H4-S4>0,E4-H4-S4,0)<=0) ,0, IF(W4+H4<IF(E4-H4-S4>0,E4-H4-S4,0) ,IF(W4+H4<0,0,W4+H4),IF (E4-H4-S4>0, IF(W4>H4,E4-S4,H4+AF4) ,H4)))
I have created the equations in SQL to sort out the following:
W4.value1
H4.value2
E4_H4_S4.value3
AF4.value4
(as those were already determined earlier in the stored procedure. So I just put them back in as Outer applies for this specific subquery)
However, I have no real idea how to read this Excel formula, and the person that wrote it was fired, so I can't go to her for help.
To me, it reads like this:
=IF(AND (W4+H4<=0,IF(E4-H4-S4>0,E4-H4-S4,0)<=0),0, IF(W4+H4< (IF(E4-H4-S4>0 then E4-H4-S4 else 0) then (IF(W4+H4<0 then 0 else W4+H4) then (IF(E4-H4-S4>0, then (IF(W4>H4 then E4-S4 else H4+AF4) else H4)) )))))
However, I doubt this is correct, as I suck at EXCEL
Anyone able to help me decipher this?

I cannot test this currently, but I think you want something like this:
CASE
WHEN W4+H4<=0 AND (CASE WHEN E4-H4-S4>0 THEN E4-H4-S4 ELSE 0 END)<=0
THEN 0
ELSE
WHEN W4+H4< (CASE WHEN E4-H4-S4>0 THEN E4-H4-S4 ELSE 0 END)
THEN (CASE WHEN W4+H4<0 THEN 0 ELSE W4+H4 END)
ELSE (CASE WHEN E4-H4-S4>0 THEN (CASE WHEN W4>H4 THEN E4-S4 ELSE H4+AF4 END) ELSE H4 END)
END
I really had no idea how to format this, but I believe everything should work as intended according to the ridiculous IF() function, of course substituting your converted values and so on.
EDIT:
This is a straight conversion from EXCEL to SQL so please for the love of programming fix this so 1. someone else can read it (i.e. what if you got fired and you left the next poor soul the same thing you got stuck with) and 2. get rid of the unnecessary checks.

Related

SQL - display N/A if the rundate is after a specific date

this is my first time posting. I am trying to display "N/A" when the the date I selected to let the query run is after a specific date, otherwise I do want a calculated number, but I keep receiving error messages. Can someone help me with this?
CASE When a.run_date > a.street_day
THEN convert(varchar(10),pre_order_forecast) as pre_order_forecast
pre_order_forecast = 'N/A'
ELSE (CASE
WHEN datediff(DAY, fcst.forecast_creation_day, NVL(a.street_day)) BETWEEN 0 AND 7
THEN fcst_qty_wk0
END)
END AS pre_order_forecast
Typically, a CASE expression is along the lines of
> CASE
> WHEN condition test 1 THEN value/calculation
> WHEN condition test 2 THEN value/calculation
> ...
> ELSE value/calculation END
It goes through the WHEN condition tests in sequence, until one is true; then that value/calculation is used (it's like a series of IF THEN ELSE expressions).
Writing these quickly becomes second nature imo, but to get started, you can
Start by writing CASE
On the next line, write WHEN, some spaces, and then THEN
Fill in the thing to check whether true after the WHEN, and what the result would be if it was true after the THEN
Repeat with the next line etc
Add an 'ELSE' for when none of the checks are true.
In your case, I'm guessing you want something like
CASE WHEN a.run_date > a.street_day
THEN convert(varchar(10),pre_order_forecast)
WHEN datediff(DAY, fcst.forecast_creation_day, NVL(a.street_day)) BETWEEN 0 AND 7
THEN fcst_qty_wk0
ELSE 'N/A'
END AS pre_order_forecast
The exact syntax and fields depend on your database.
Note that in the second WHEN, you may need to also convert fcst_qty_wk0 to varchar to match the other outputs.

AVG function DB2 SQL run time error

I run this sql statemnt in DB2
SELECT (CASE WHEN AVG(TVAPC) > 0 THEN 'Y' ELSE 'N' END)
FROM TABLE1
So far it was working.. TVAPC column is char(5).
But on run time we got that error. sql error +364 error. And we can't catch that error familiar case. Sometimes we get sometimes we cant for same case.
It seems run time error, but how can i prevent that error?
Thank you so much!
I don't have enough reputation to comment your post yet, so I'm going to post an "answer" with the risk of the stackoverflow royalty voting it down and trashing me for doing so. Check the data type of the column. When I run it, it works fine as long as the columns data type is a numeric type. If it happens to be your case (that the col. type isn't numeric) cast/convert it to Int.
--In case it isn't a numeric type...
SELECT (CASE WHEN AVG(CAST(column as int))>0 THEN 'Y' ELSE 'N' END)
FROM TEMP1234
Why are you taking the average of a string? You should fix the data format.
In any case, you can check if the value has a number. Here is one way (assuming the value is a number):
SELECT (CASE WHEN AVG(CASE WHEN LENGTH(TRANSLATE(test_str, '*', ' 0123456789')) = 0 THEN TVAPC END) > 0
THEN 'Y' ELSE 'N'
END)
FROM TABLE1

Syntax Error in Simple Case Statement

Trying to use a Case Statement for the first time, which is why the code is so small/simple. Returning a syntax error, supposedly with my table name [Impact].
Select Case [Impact]
Case Is = 0
[New Impact] = "1"
End Select
Any assistance is appreciated. I've looked around for solutions, but most of the time answers are related to something else in their code, and not anything I have in this small test code.
In MS Access, the logic one would normally used is:
Select iif([Impact] = 0, "1", NULL) as [New Impact]
You could use switch() as well, but that seems like overkill.
The Microsoft Access Case statement can only be used in VBA code.
The structure is like so:
Select Case test_expression
Case condition_1
result_1
Case condition_n
result_n
[Case Else
result_else]
End Select
My guess, is you missed the part about using this through VBA only.

Divide by zero error encountered in SQL Server 2014

In this specific part of a query I'm trying to create, I'm getting a "divide by zero" error. I believe this is because I'm trying to divide ARTran.Qty by a null. I'm unsure how I can work around this... (I'm using Cast as in the next part of the query I'm trying to using this number as a decimal
(CASE WHEN ([ARTran].[UOM] LIKE 'EACH')
THEN CAST([ARTran].[Qty] AS VARCHAR(30))
ELSE CAST((ARTran.Qty / InventoryItem.UsrLength) AS VARCHAR(30))
END)
Can someone point me in the right direction? I've looked at a few other threads on this, but I'm still not sure how to solve this.
Use NULLIF to get around this.
(CASE WHEN ([ARTran].[UOM] LIKE 'EACH')
THEN CAST([ARTran].[Qty] AS VARCHAR(30))
ELSE CAST((ARTran.Qty / NULLIF(InventoryItem.UsrLength, 0) AS VARCHAR(30))
END)

T-SQL 2005 - Divide by zero error encountered

I am trying to get some percentage data from a stored procedure using code similar to the line below. Obviously this is going to cause a (Divide by zero error encountered) problem when base.[XXX_DataSetB] returns 0 which may happen some of the time.
Does anyone know how i can deal with this in the most efficient manner?
Note: There would be about 20+ lines looking like the one below...
cast((base.[XXX_DataSetB] - base.[XXX_DataSetA]) as decimal) /
base.[XXX_DataSetB] as [XXX_Percentage]
I guess it depends on what behaviour you expect when XXX_DataSetB is zero.
If you don't want any rows returned that might cause an error, you can easily filter out those rows by adding where XXX_DataSetB <> 0 to your query.
If you would like problem rows to be NULL then you could use a case statement, or something like this:
cast(([XXX_DataSetB] - [XXX_DataSetA]) as decimal) / nullif([XXX_DataSetB], 0)
I'd force the average to null by making the bottom term null instead of zero:
cast((base.[XXX_DataSetB] - base.[XXX_DataSetA]) as decimal)
/ case when base.[XXX_DataSetB] = 0 then null
else base.[XXX_DataSetB] end as [XXX_Percentage]