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

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 )

Related

Problem with field not equal to null in case statement

So I have EXISTS in huge query which looks like this:
EXISTS(
SELECT
*
FROM
ExistTable
WHERE
ExTableFieldA = #SomeGuid AND
ExTableFieldB = MainTableFieldB AND
ExTableFieldA <> (
CASE
WHEN MainTableFieldZ = 10 THEN MainTableFieldYYY
ELSE NULL
END
)
)
The problem comes from ELSE part of CASE statement, this ExTableFieldA <> NULL will be always false. I could easily write another parameter #EmptyGuid and make it equal to '00000000-0000-0000-0000-000000000000' and everything will work but is this the best approach ?
Pretty much I want to execute another check into the exist for the small size of the records which return the "main" query.
How about removing the case and just using boolean logic?
WHERE ExTableFieldA = #SomeGuid AND
ExTableFieldB = MainTableFieldB AND
(MainTableFieldZ <> 10 OR ExTableFieldA <> MainTableFieldYYY)
I would also recommend that you qualify the column names by including the table alias.
Note: This does assume that MainTableFieldZ is not NULL. If that is a possibility than that logic can easily be incorporated.
ELSE NULL is implied even if you don't list it, but you could use ISNULL here.
ISNULL(ExTableFieldA,'') <> (
CASE
WHEN MainTableFieldZ = 10 THEN MainTableFieldYYY
ELSE ''
END
)
You may need to use some other value like 9999 instead of ''

Case inside 'where' section that changes the condition of an AND statement

I am creating a store procedure and i am wondering how can i add a case block in an Add statement inside the where statement.That case statement checks an input parameter and depending its value it will change the condition from greater that to smaller than and of course be added to the add conditions
So a part of the query is like:
WHERE
AND BM.Example1 IS NOT NULL
AND BM.Example2 IS NOT NULL
AND ( Case When #inputParamter= 'A' THEN AND BM.Example < 0 ELSE And BM.Example> 0 )
ORDER BY 'SEG' ASC, 'CCY' ASC
So by this approach i am thinking to extract an add statement depending on the input parameter but unfortunately i keep getting syntax errors.
Is that possible?
Yepp, just use this:
AND (( #inputParamter= 'A' AND BM.Example < 0) OR ( #inputParamter<>'A' AND BM.Example> 0) )
However, be carefull with NULL, you have to put it in the logic as a third option.
here is a similar answer using case
AND ( Case When #inputParamter = 'A' AND BM.Example < 0 THEN 'Y'
When #inputParamter <> 'A' AND BM.Example > 0 THEN 'Y' ELSE 'N' END = 'Y')

How to evaluate expression in standard sql?

I have an alphanumeric expression which I like to evaluate to true/false.
For example ('A' = 'B' or 10 > 5) should return true.
I am working with DB2 for i, so a standard sql would be required.
I tried
Select ('A' = 'B' or 10 > 5) from sysibm/sysdummy1
and
Select (('A' = 'B' or 10 > 5) = '1') from sysibm/sysdummy1
but the error says in the first case Token '(' required and in the second case Token '=' invalid.
How would you do this?
Thanks
Try like this please:
select case when 'A' ='B' or 10 > 5 then 1 else 0 end
Also maybe this post can help Boolean Expressions in SQL Select list

SQL Case statement with 'or' and 'and'

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.

Y or N in an SQL result

Is there a way to have a query return either a 'Y' or 'N' for a column if that column has a value above a certain number say '25'.
I am using DB2 on ZOS
SELECT CASE WHEN Col1 > 25 THEN 'Y' ELSE 'N' END As Value1
FROM Table1
For MySQL:
SELECT IF(column > 25, 'Y', 'N')
FROM table
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if
There is a IF block for SQL.
http://sqltutorials.blogspot.com/2007/06/sql-ifelse-statement.html
Use the SQL CASE statement
SELECT CASE WHERE columnname > 25 THEN 'Y' ELSE 'N' END FROM table;
select
case when somecolumn > 25 then 'Y' else 'N' end as somename
from sometable;
Select If(myColumn > 25, 'Y', 'N') From myTable
Apart from the IF and CASE statements another valid alternative is to create a user defined function feeding in your column value and returning Y or N.
This has the not inconsiderable advantage that if you are selecting on this criteria in more than on SQL statement and at a later date your condition changes - to over 30 say - then you have only one place to change your code. Although using a function adds complexity and overhead so is not always optimal don't underestimate the maintenance advantage of this approach. A minor plus too is you can make the name of the function something more meaningful, and hence self documenting, than an inline CASE