Setting variables on CASE results in SQL? - sql

I'm sure the answer is simple, but I have been searching for a while and can't seem to get this working ...
I am tring to set a variable based on a Case statement result.
So I am checking length of two columns
dbo.MY_TABLES.MY_RULESET,
dbo.MY_TABLES.MY_SPECIFICATION
My case statements check If length/75 is over 1 then we return 1. If under 1, then we return the value (under 1)
I then want to add these two results together and is where im getting problems when i need the addtion dependant on the case results.
Here is sample
Declare #SpecValue FLOAT
Declare #RuleValue FLOAT
SELECT
CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75 as Score1,
CAST(LEN (MY_RULESET) AS FLOAT)/75 AS Score2,
CASE
WHEN (CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75,2))
END as Spec1_RuleLength,
CASE
When (CAST(LEN (MY_RULESET) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN (MY_RULESET) AS FLOAT)/75,2))
END as Eval2_RuleLength,
dbo.MY_TABLES.MY_RULESET,
dbo.MY_TABLES.MY_SPECIFICATION
FROM MY_TABLES

All columns in a SELECT clause are computed as if they're evaluated in parallel. This means that one column cannot depend on the value of another column computed in the same SELECT.
You might try moving the current query into a subselect:
SELECT Score1 + Score2 /* etc */
FROM (
SELECT
CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75 as Score1,
CAST(LEN (MY_RULESET) AS FLOAT)/75 AS Score2,
CASE
WHEN (CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN(MY_SPECIFICATION) AS FLOAT)/75,2))
END as Spec1_RuleLength,
CASE
When (CAST(LEN (MY_RULESET) AS FLOAT)/75) >1 Then '1'
ELSE (Round(CAST(LEN (MY_RULESET) AS FLOAT)/75,2))
END as Eval2_RuleLength,
dbo.MY_TABLES.MY_RULESET,
dbo.MY_TABLES.MY_SPECIFICATION
FROM MY_TABLES
) t

Related

NULL values not filtered out with WHERE statement

SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE
When I select these columns, I see that the results have some NULL values. They don't seem to be strings. However, when I try to filter the NULL values out with a where statement:
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE VOLUME = NULL
I don't see any results. What might be the possible causes? I also tried filtering with 'NULL' but that would throw an error since the column type is double.
use this for only want null recode
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE VOLUME IS NULL
or
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE ISNULL(VOLUME,'') = ''
if you get not null value then use
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE ISNULL(VOLUME,'') <> ''
or
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE VOLUME IS NOT NULL
a more full answer is NULL is not directly comparable, much like NaN is not comparable in floating point numbers. Both represent the "lack of a value" if you "have not value here" how can you compare it to something.
"There is nobody next to you, what is their name?" it just doesn't make sense.
So to test you ask column IS NULL or column IS NOT NULL or you can use a compact logic expression see Conditional Expressions but some common ones in Snowflake are:
short form
ANSI long
snowflake long
NVL(column,'')
CASE WHEN column IS NOT NULL THEN column ELSE '' END
IFF(column IS NOT NULL, column, '')
NVL2(column,'a','b')
CASE WHEN column IS NOT NULL THEN 'a' ELSE 'b' END
IFF(column IS NOT NULL, 'a', 'b')
ZEROIFNULL(column)
CASE WHEN column IS NOT NULL THEN column ELSE 0 END
IFF(column IS NOT NULL, column, 0)
COALESCE/NVL/IFNULL are all interchangable so I will only show one (expect COALESCE can handle N items which are checked in order)
You can use the where is function or is not function to filter all the null values.

Operator 'is true' for type 'long' not found for oracle sql query

I am trying to run this logic, where i get output as follows,
My code to get the below output field is shown below
select max(dtm) over (partition by name ,id )-current_date from mm
output
-4168
-4168
-4168
-4127
what i want is to run this logic along with 'case when' statement so i tried:
case when max(dtm) over (partition by name ,id )-current_date then 'yes'
else 'No' end as output
from mm
but i get an error as follows, not sure what went wrong in this logic.
Operator 'is true' for type 'long' not found
There are two forms of CASE expression. One is referred to as simple case expression and the other is referred to as searched case expression. The SQL in your question uses the latter, i.e. searched case expression. I believe you probably need simple case expression, i.e.
select case max(dtm) over (partition by name ,id ) - current_date
when -4168 then 'Yes'
else 'No'
end as answer
from mm
select
case when current_date-max(dtm) over (partition by name ,id ) < 30 then 'yes'
else 'No' end as 'output'
from mm

Using SUM in CASE if value is numeric

I have a similar situation to the CASE clause well known problem:
DECLARE #i INT = 1;
SELECT CASE WHEN #i = 1 THEN 1 ELSE MIN(1/0) END;
In that case the code will throw an exception, Divide by zero error encountered, even though in theory you would never reach that min(1/0) scenario.
So I have similar situation:
CASE WHEN CodeValue in ('Numeric1','Numeric2') THEN SUM(cast(VarcharValue as int)) ELSE max(VarcharValue) END
In other words I need to use SUM function if VarcharValue is numeric (it could be determined by CodeValue if it is numeric), if that's not the case, I need return VarcharValue with MAX function.
Any suggestions?
You have a strange construct, because one case branch returns a string and another a number. SQL Server decides, in such a case, that the expression returns a number.
And you'll have problems with the ELSE, because a non-numeric string will be converted to a number. Error.
The following might work:
(CASE WHEN CodeValue in ('Numeric1', 'Numeric2')
THEN CAST(VARCHAR(MAX), SUM(CAST(VarcharValue as int)))
ELSE MAX(VarcharValue)
END)
You might still have a problem, if the SUM() is being calculated over all data before filtering (which I think is possible with an aggregation query). My recommendation is to upgrade to a supported version of SQL Server and use:
(CASE WHEN CodeValue in ('Numeric1', 'Numeric2')
THEN CAST(VARCHAR(MAX), SUM(TRY_CAST(VarcharValue as int)))
ELSE MAX(VarcharValue)
END)
However, you can repeat the CASE logic:
(CASE WHEN CodeValue in ('Numeric1', 'Numeric2')
THEN CAST(VARCHAR(MAX), SUM(CASE WHEN CodeValue in ('Numeric1', 'Numeric2') THEN CAST(VarcharValue as int) END))
ELSE MAX(VarcharValue)
END)

SQL - get only numbers including minus value not alphabets and character from a column

I have a column which has numbers(including negative values),alphabets,alphanumeric and single character symbol(like -,&,#).
How to get rid of alphabets,alphanumeric and symbols and get the sum of the column.
I have to use the condition only in select statement not in where condition. Because it should not affect other column results.
This is what I've tried:
SELECT COUNT(*), CASE
WHEN REGEXP_LIKE(REMD_PART_CSN, '^-?[0-9]\d*(\.\d+)?$') THEN SUM(REMD_PART_CSN)
ELSE NULL
END
FROM <TABLE>
How about this?
SELECT
COUNT(*),
SUM(
CASE
WHEN REGEXP_LIKE(REMD_PART_CSN, '^-?\d+(\.\d+)?$')
THEN CAST(REMD_PART_CSN AS NUMBER)
ELSE 0
END
)
FROM yourtable
I.e. for every row, if REMD_PART_CSN looks like a number, then convert it to a number, else use 0 instead. At the end, return the sum of all those values.

How do I create an If-Then-Else in T-SQL

I have some negative values coming back from a query. I would like them to just be zero.
How do I write a condition in my sql query that returns zero if the value is below a certain value.
sol:
CASE WHEN CONVERT(float,dt.FQI53X02_101) < 1.3 THEN 0 ELSE CONVERT(float,dt.FQI53X02_101) END AS FQI53X02_101
You dont use If-Then-Else in the actual query (you can use them but thats something else)...
What you use is a Case statement... Try this
Select
Case When [Value] < 0 Then 0 Else [Value] End
From
Example
If you want it as part of your query, wrap the return inside a CASE statement. Example from MSDN is below
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price
( ABS(Value) + Value ) / 2
edit - this doesn't work now the question has changed