SQL Case statement with 'or' and 'and' - sql

Can I use a case statement as follows?
CASE
WHEN (condition1 = 1 or 2)
AND condition2 = 3
THEN result = 'Result'
ELSE
NULL
END
Conditions 1 and 2 will be looking for different values, just fyi.
If not, is there a better way to write this?
Thank you!

Would this work?
CASE WHEN condition1 in (1, 2) AND condition2 = 3
THEN 'Result'
ELSE NULL
END
AS result

Well it depends on the system you are using. For example if you use Oracle and PL/SQL you can check the statement here
http://www.techonthenet.com/oracle/functions/case.php
What DB are you using? And do you want the statement in SQL or in some other kind of code or stored procedure?

I'm not sure what you want to do with the statement. In a select statement, it would be:
SELECT (CASE WHEN (condition1 = 1 or 2) AND condition2 = 3
THEN 'Result'
END) as result
You don't need the else because NULL is returned by the statement automatically if none of the when conditions are met.
In a where, it would be:
WHERE (condition1 = 1 or 2) AND (condition2 = 3) AND (result = 'Result')
The else condition is equivalent to false.

Related

Oracle SQL filtering results from a query

I'm trying to write a query to filter results from two columns of a parameterised query.
CASE WHEN REGEXP_INSTR('%param1)s', '[|(\\\[]') > 0 AND REGEXP_LIKE(column1.'%param1)s')
THEN 'Y' END REGEXP_MATCH,
CASE WHEN REGEXP_INSTR('%param1)s', '[|(\\\[]') = 0 AND column1 LIKE '%(param1)s'
THEN 'Y' END LIKE_MATCH,
This creates two columns, REGEXP_MATCH and LIKE_MATCH.
I then want to filter from both of these columns. If REGEXP_MATCH is 'Y' then use a regular expression on param1. If LIKE_MATCH is 'Y' then use a LIKE operator on param1, else use '*' and match everything.
I'm not entirely sure what the best way to do this is though.
Is this what you want?
WHERE ( REGEXP_INSTR('%param1)s', '[|(\\\[]') > 0 AND REGEXP_LIKE(column1.'%param1)s')
) OR
(REGEXP_INSTR('%param1)s', '[|(\\\[]') = 0 AND column1 LIKE '%(param1)s'
) OR
(%param1 IS NULL)

Conditional Where Statement

I am trying to setup a conditional statement that will determine which WHERE clause to use.
I need to check 2 separate conditions, one on each side of the AND and if results are greater than 1 then use a particular statement if not then use nothing
Like this logic but in pdw sql
WHERE
if cte1.counte > 1 then 'cte1.built is not null' else ''
AND
if cte2.countd > 1 then 'cte2.demo is not null' else ''
possible combinations:
WHERE CTE1.BUILD IS NOT NULL
WHERE CTE1.BUILD IS NOT NULL AND CTE2.DEMO IS NOT NULL
WHERE CTE2.DEMO IS NOT NULL
BLANK
Is this possible to do?
Thanks in advance
Something like this:
WHERE (cte1.counte > 1 and cte1.built is not null or cte1.counte <= 1) and
(cte2.countd > 1 and cte2.demo is not null or cte2.countd <= 1)

CASE WHEN ... THEN

I'm trying to edit a code someone wrote some months ago, but I can't understand some parts, for example:
CASE
WHEN #PROMPT('SEL_TYPE')# = '%' then 1
WHEN #PROMPT('SEL_TYPE')# = 'ALL' then 1
WHEN e.evt_job = #PROMPT('SEL_TYPE')# then 1
ELSE 0
END = 1
Wherever I read about how CASE .. WHEN works, it's like:
CASE A
WHEN 'ok' THEN C = 'ok'
WHEN 'bad' THEN C = 'bad'
But im my example it's just THEN 1 or ELSE 0.
Whats the meaning of that 1 or 0? It's something I'm missing on the code, or that 1 or 0 means something?
Thanks all, and sorry for my English :)
You should understand the difference between the CASE expression and the CASE statement. This is a CASE expression:
CASE
WHEN #PROMPT('SEL_TYPE')# = '%' then 1
WHEN #PROMPT('SEL_TYPE')# = 'ALL' then 1
WHEN e.evt_job = #PROMPT('SEL_TYPE')# then 1
ELSE 0
END = 1
This is an incomplete CASE statement (Supported by other databases, like Oracle or MySQL, but not SQL Server):
CASE A
WHEN 'ok' THEN C = 'ok'
WHEN 'bad' THEN C = 'bad'
An expression is something that can be evaluated on the right hand side of an assignment, or in a SELECT statement, for instance.
A statement is a command that can be used in an imperative language, i.e. in a stored procedure. The CASE statement (if supported by a database) works just like an IF statement.
It means someone has overcomplicated things. If SQL Server had a boolean data type, they'd probably have just had then true, else false and no comparison at the end. But because that's not possible in SQL Server, they've substituted 1 and 0 and then just compare that to 1 at the end to make it a logical comparison.
It could equally have been written as:
#PROMPT('SEL_TYPE')# = '%' OR
#PROMPT('SEL_TYPE')# = 'ALL' OR
e.evt_job = #PROMPT('SEL_TYPE')#
With no need for a CASE expression at all.
Or even, probably, as just #PROMPT('SEL_TYPE')# IN ('%','ALL',e.evt_job), but some may feel that this obscures the intent a little too much.
So,
select code, wo_num, desc from table1
where org = #PROMPT('SEL_ORG')# and
CASE
WHEN #PROMPT('SEL_WO_TYPE')# = '%' then 1
WHEN #PROMPT('SEL_WO_TYPE')# = 'ALL' then 1
WHEN e.evt_jobtype = #PROMPT('SEL_WO_TYPE')# then 1 ELSE 0 END = 1
and e.evt_type in (''A,'B')
Could have more simply been written as:
select code, wo_num, desc from table1
where org = #PROMPT('SEL_ORG')# and
e.evt_type in (''A,'B') and
(
#PROMPT('SEL_TYPE')# = '%' OR
#PROMPT('SEL_TYPE')# = 'ALL' OR
e.evt_job = #PROMPT('SEL_TYPE')#
)
Someone wrote a CASE expression (and then had to introduce the 1s and 0s) when all they needed was basic boolean logic.

How does 'in' clause works in oracle

select 'true' from dual where 1 not in (null,1);
when we execute this which will result nothing
what my question is:
is the above query is logically equivalent to
select 'true' from dual where 1 != null and 1 != 1;
which will result nothing just as above statement
Please clarify?
Correct (but note that IN is an operator, not a clause and it works like this in SQL in general, not only for Oracle).
where 1 not in (null,1)
is equivalent to:
where 1 != null and 1 != 1
which should really be written as:
WHERE 1 NOT IN (NULL, 1)
and
WHERE 1 <> NULL AND 1 <> 1
which is the same as:
WHERE (1 <> NULL) AND (1 <> 1)
which evaluates to:
WHERE UNKNOWN AND FALSE
and further as:
WHERE FALSE
So, it correctly returns no rows.
Notice that if you had WHERE 1 NOT IN (NULL, 2), it would evaluate to WHERE UNKNOWN (left as an exercise) and no rows would be returned either.
The issue of your script in comparing with NULL value. You should use
column is null and column = 1
Actually NULL is an undefined value. Any comparation with NULL gives neither True nor False but NULL. Even NULL = NULL
That's why your 1 not in (null,1) doesn't work.
Yes they are.
select something from table where column not in (1,2,3);
is equivalent to
select something from table where column != 1 and column != 2 and column != 3;
The IN statement is a collection of OR statements, while NOT IN is a collection of AND statements - but it is also not equal to.
So the NOT IN is equivalent to:
1 <> NULL
AND 1 <> 1
AND ...
While the IN would be equivalent to:
1 = NULL
OR 1 = 1
OR ...
Note that having NULL in the collection will not work, due to the quirky nature of NULL.
Yes. It is correct. Also NULL values should be compared with IS NULL

Does anyone know how to write this SQL If statement better?

I have this IF statement in my Stored Procedure:
if ((#someParam = 'C') OR (#someParam = 'I' AND #SomeOtherParam <> 2 AND #SomeOtherParam <> 4 AND #SomeOtherParam <> 5))
My question is can I check #SomeOtherParam in one shot, instead of having to check it 3 separate times?
This should do the trick:
if ((#someParam = 'C') OR (#someParam = 'I' AND #SomeOtherParam NOT IN (2,4,5)))
IN takes a list of values, and returns true if your value is found in the list. Adding NOT means that it will return true if your value is not found.
maybe something with CASE statements:
if case #someparam when 'C' then 1 when 'I' then #someotherparam NOT IN (2,4,5) else 0 end
Try
if (#SomeOtherParam NOT IN (2, 4, 5))
There is also EXISTS keyword in SQL which you can use
if not EXISTS (select * from list where my_column= #someotherparam )