check if value is numeric in sql view - sql

What is wrong with my case statement below?
If the value is numeric then I want to check if its greater than or equal to 5 and return 1 if true else return 0. Is there any elegant way of doing this?
Below CASE is nested in another CASE
CASE
WHEN(
xyz <> a
AND abc <> 3
AND
CASE
WHEN ISNUMERIC(LEFT(o.RepCode, 1)) = 1 THEN
CASE
WHEN CONVERT(INT, LEFT(o.RepCode, 1)) >= 5 THEN 1
ELSE 0
END
ELSE 0
END
)
THEN 1
ELSE 0
I get the below error on the first CASE after the second AND before WHEN ISNUMERIC()
An expression of non-boolean type specified in a context where a condition is expected.

Missing criteria in your outermost case statement:
CASE
WHEN(
xyz <> a
AND abc <> 3
AND
CASE
WHEN ISNUMERIC(LEFT(o.RepCode, 1)) = 1 THEN
CASE
WHEN CONVERT(INT, LEFT(o.RepCode, 1)) >= 5 THEN 1
ELSE 0
END
ELSE 0
END = ? -- Missing criteria
)
THEN 1
ELSE 0
Edit: Not sure there's an ideal way to format this, but I find the following to be easier to follow:
CASE WHEN ( xyz <> a
AND abc <> 3
AND CASE WHEN ISNUMERIC(LEFT(o.RepCode, 1)) = 1
THEN CASE WHEN CONVERT(INT, LEFT(o.RepCode, 1)) >= 5
THEN 1
ELSE 0
END
ELSE 0
END = ? -- Missing Criteria
)
THEN 1
ELSE 0
END

Related

Logical Values in SQL SELECT clause

I would like to ultimately write a query that would return the "truth table" result of various LHS operand RHS logical operations. I would expect to see a final result looking something like
LHS
op
RHS
result
0
>
0
0
0
>
1
0
0
>
Null
Null
1
>
0
1
1
>
1
0
1
>
Null
Null
Null
>
0
Null
Null
>
1
Null
Null
>
Null
Null
0
<=
0
1
0
<=
1
1
0
<=
Null
Null
1
<=
0
0
1
<=
1
1
1
<=
Null
Null
Null
<=
0
Null
Null
<=
1
Null
Null
<=
Null
Null
0
<>
0
0
0
<>
1
1
0
<>
Null
Null
1
<>
0
1
1
<>
1
0
1
<>
Null
Null
Null
<>
0
Null
Null
<>
1
Null
Null
<>
Null
Null
I was initially stymied in this quest by not being able to simply return a True/False or 1/0 from a simple select statement. If, for instance, I wish to return the truth table result (1 or True) of 1>0, I cannot simply
SELECT 1>0, but instead have to do something akin to
SELECT
1 LHS, '>' op, 0 RHS,
CASE WHEN (1>0) -- replace 1>0 with logical check of choice
THEN 1
WHEN NOT (1>0) -- remember to do so here also
THEN 0
ELSE NULL
END result
, or the equivalent IIF statement; both the CASE and IIF solutions would require the equation twice - first directly, then within a NOT negation to check for NULL values as possible returns.
Generalizing this to LHS and RHS, we have
SELECT
A.LHS, A.op, A.RHS,
CASE WHEN (A.LHS>A.RHS) -- replace LHS>RHS with logical check of choice
THEN 1
WHEN NOT (A.LHS>A.RHS) -- remember to do so here also
THEN 0
ELSE NULL
END
FROM
Value_Table A
Setting the operator value is something that I also haven't figured out as the operators themselves have to be (AFAIK) stored as strings, so
SELECT
CASE WHEN (A.LHS A.op A.RHS)
THEN 1
WHEN NOT (A.LHS A.op A.RHS)
THEN 0
ELSE NULL
END
FROM
Value_plus_Op_Table A
obviously won't work.
Doing
SELECT
CASE A.op
WHEN '>'
THEN CASE
WHEN (A.LHS > A.RHS)
THEN 1
WHEN NOT (A.LHS > A.RHS)
THEN 0
ELSE NULL
END
WHEN '<='
THEN CASE
WHEN (A.LHS <= A.RHS)
THEN 1
WHEN NOT (A.LHS <= A.RHS)
THEN 0
ELSE NULL
END END result
FROM
Value_plus_Op_Table A
is not a general solution, as each op code must be changed to the appropriate context on an individual basis.
On this server I DO have access to do sp_executesql, but I can't simply sp_executesql #statement=CAST(A.LHS as NVarchar(50))+' '+A.op+' '+(A.RHS as NVarchar(20)) and expand that in the two places in the code that LHS op RHS appears, but would instead need to build the whole query into a string and execute that; and I'm not sure how I would execute that for a list of operands in a single query.
So, how can this query be generalized to encompass multiple operands and data types?

How to use case function in SQL where clause?

I am converting a Crystal Report to SSRS. In the CR Select Records section there are several filters including an IF statement like this:
(if BeginCompany <> 0 and EndCompany <> 999 then
Company between BeginCompany and EndCompany else 1 = 1)
How can I make an equivalent CASE statement in an SQL WHERE clause in my dataset? Thanks.
Are BeginCompany and EndCompany actually variables? What are they when you don't want to use them as a filter? NULL? Something else?
Maybe you're looking for this (assuming companies are represented by positive integers):
WHERE Company >= COALESCE(#BeginCompany, 0)
AND Company <= COALESCE(#EndCompany, 2147483647)
Which is just another way to express:
WHERE Company >= CASE
WHEN #BeginCompany IS NULL THEN 0 ELSE #BeginCompany END
AND Company <= CASE
WHEN #EndCompany IS NULL THEN 2147483647 ELSE #EndCompany END
You can't use a CASE expression for control-of-flow, e.g.
CASE ... something ... THEN Column BETWEEN x AND y
CASE is an expression that returns a single value. Much more background here:
Dirty Secrets of the CASE Expression
It sounds like the logic wants the Company field to be between the BeginCompany and EndCompany parameters if BeginCompany <> 0 and EndCompany <> 999 to show a record but if they do equal 0 or 999, then show all records. If a users enters 0 for the Begin parameter or 999 for the end, then the query would show all records.
I think in SQL it would be
WHERE (
#BeginCompany <> 0
and #EndCompany <> 999
AND Company between #BeginCompany and #EndCompany
)
OR (
#BeginCompany = 0
OR
#EndCompany = 999
)
If you really wanted to squeeze a CASE statement if there then
WHERE CASE WHEN
#BeginCompany <> 0
and #EndCompany <> 999
AND Company between #BeginCompany and #EndCompany
THEN 1
WHEN
#BeginCompany = 0
OR
#EndCompany = 999
THEN 1
ELSE 0 END = 1

Bigquery if null and 0 then condition

I would like to add a condition if the row value is null or 0 then 0 else 1.
SELECT CASE WHEN NUM_FRIEND IS NULL AND 0 THEN 0 ELSE 1 END AS friends_column
FROM TABLE
I get the following error:
No matching signature for operator and
As a case expression, the syntax is:
SELECT (CASE WHEN NUM_FRIEND IS NULL OR NUM_FRIEND = 0 THEN 0 ELSE 1 END) AS friends_column
FROM TABLE;
You can simplify this to:
SELECT (CASE WHEN NUM_FRIEND <> 0 THEN 1 ELSE 0 END) AS friends_column
FROM TABLE;
Or, instead of 0 and 1, return a boolean:
SELECT (NUM_FRIEND <> 0) AS friends_column
FROM TABLE;

How to add multiple case in sql

I want to add multiple cases in single line. In other words in below example if first item is NOT NULL and second item is equal to 1 then output should be 1 else 0. How can I do it?
MatchbyCatalog= (case when ISNULL(pc.ProductID,0) and tc.RawMatch=1) then 1 else 0 end)
Is this what you want?
CASE WHEN pc.ProductID IS NULL and tc.RawMatch=1 THEN 1 ELSE 0 END
You misunderstood ISNULL() , it's a function that replaces the value if it's NULL, it's not a condition . You were looking for IS NULL
That's all you need.
MatchbyCatalog= (case when ISNULL(pc.ProductID,0)=1 and tc.RawMatch=1 then 1 else 0 end)
when you say both I think you mean pc.ProductID and tc.RawMatch...
I have not you're entire sql query so I have to guess.
You have multiple solutions :
Nearly ss you have wrote when pc.ProductId = 1 and tc.RawMatch=1 then 1 else 0 :
MatchbyCatalog= (case when ISNULL(pc.ProductID,0)=1 and tc.RawMatch=1) then 1 else 0 end)
You could also use a trick : 1 * 1 should be 1 and 1 * 0 =0 so you could write (if tc.Rawmatch is a boolean sort of with values 0 or 1) :
MatchbyCatalog= ISNULL(pc.ProductID,0) * tc.RawMatch
case when {Condition} then {result}
When {Condition} then {result}
Else {result}

query sql returning custom value

I have a table with 3 columns (containing Integer values that assume only values from 0 to 10). I want extract, with a single query, a table with 1 column. This column must assume a value based on the following logic:
If one of these three columns has value 0 ----> the value of column of table generated by query must be 0 too.
If none of the last three columns has value 0 ----> the value of column must assume the value 1.
You are looking for CASE construct or IF function:
SELECT CASE WHEN (t.field1 = 0 OR t.field2 = 0 OR t.field3 = 0) THEN 0
ELSE 1 END AS value
FROM t;
In this specific case you might also use the fact that any member being zero will zero the product:
SELECT CASE WHEN (t.field1*t.field2*t.field3 = 0) THEN 0 ELSE 1 END AS value
FROM t;
Or
SELECT IF((t.field1*t.field2*t.field3)=0, 0, 1) AS value FROM t;
This is a simple case statement. Assuming there are no NULL values, try this:
select (case when col1 = 0 or col2 = 0 or col3 = 0 then 0 else 1 end)
Try this
SELECT
CASE
WHEN column1 = 0 THEN 0
WHEN column2 = 0 THEN 0
WHEN column3 = 0 THEN 0
ELSE 1
END
FROM urtable