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;
Related
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?
This question already has answers here:
Conditional Count on a field
(8 answers)
Closed last year.
I'm working with SQL Server and I want to create a view (my_View) which has these columns :
[element_1]
[element_2]
[element_3]
[element_4]
All of them are referring to the same column, named [parent_1], in another table (AT)
[parent_1] can have 4 possible values [value_1], [value_2], [value_3] and [value_4].
What I want is that, using COUNT,
[element_1] is equal to the number of times that [parent_1] is equal to [value_1]
same for [element_2], [element_3] and [element_4] equals to [value_2], [value_3] and [value_4].
Is it possible to use "==" inside the COUNT to see if it checks the criteria?
Something like this:
COUNT (AT.parent_1 == "value_1") AS element_1
COUNT (AT.parent_1 == "value_2") AS element_2
COUNT (AT.parent_1 == "value_3") AS element_3
COUNT (AT.parent_1 == "value_4") AS element_4
Thanks guys
You can use the CASE instruction for that
https://learn.microsoft.com/fr-fr/sql/t-sql/language-elements/case-transact-sql?view=sql-server-ver15
SUM (CASE WHEN AT.parent_1 = 'value_4' THEN 1 ELSE 0 END) as element_4
You can do something like:
SELECT
sum(case when parent_1 = "value_1" then 1 else 0 end) as element_1,
sum(case when parent_1 = "value_2" then 1 else 0 end) as element_2,
sum(case when parent_1 = "value_3" then 1 else 0 end) as element_3,
sum(case when parent_1 = "value_4" then 1 else 0 end) as element_4
FROM table;
create my_view as
select
-- other list of columns,
SUM (CASE WHEN AT.parent_1 = 'value_1' THEN 1 ELSE 0 END) as element_1,
SUM (CASE WHEN AT.parent_1 = 'value_2' THEN 1 ELSE 0 END) as element_2,
SUM (CASE WHEN AT.parent_1 = 'value_3' THEN 1 ELSE 0 END) as element_3,
SUM (CASE WHEN AT.parent_1 = 'value_4' THEN 1 ELSE 0 END) as element_4
from tableName AT
There is no need to use == like that of a programming language, in SQL comparison operator is =
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}
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
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