IF Inside WHERE with stored procedure - sql

I have a WHERE statement that looks like this:
WHERE
((#Value1 IS NULL AND [value1_id] IS NULL) OR [value1_id] = ISNULL(#Value1, [value1_id]))
AND
((#Value2 IS NULL AND [value2_id] IS NULL) OR [value2_id] = ISNULL(#Value2, [value2_id]))
AND
((#Value3 IS NULL AND [value3_id] IS NULL) OR [value3_id] = ISNULL(#Value3, [value3_id]))
AND
((#Value4 IS NULL AND [value4_id] IS NULL) OR [value4_id] = ISNULL(#Value4, [value4_id]))
AND
((#Value5 IS NULL AND [value5_id] IS NULL) OR [value5_id] = ISNULL(#Value5, [value5_id]))
AND
((#Value6 IS NULL AND [value6_id] IS NULL) OR [value6_id] = ISNULL(#Value5, [value6_id]))
I need to add some conditional logic inside of the WHERE so I can do special things with Value5 and Value6. Basically, if Value5, Value6, or another value is null, I want to use the Value 5 and Value 6 lines as is. If all three values aren't NULL, I need to run some calculations on the values.
Any ideas on what the best action would be?

Use CASE
"CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING."
http://msdn.microsoft.com/en-us/library/ms181765.aspx
You can perform calculations inside of a SELECT like this:
SELECT
CASE
WHEN #Value7 is not null THEN #Value7 * 100
ELSE Value7 * 100
END
FROM #T
WHERE (#Value1 is null and Value1 is null)
AND (#Value2 is null and Value2 is null)
It's still not very clear what exactly you're trying to accomplish. Is the WHERE not inside a SELECT? Provide a clear, concise example if the above is not correct.

Basically, if Value5, Value6, or another value is null, I want to use
the Value 5 and Value 6 lines as is. If all three values aren't NULL,
I need to run some calculations on the values.
I think this will be hard to do with for example a CASE inside the WHERE clause. You might be better off with an IF construction outside of your SELECT statements:
IF (#Value5 IS NULL OR #Value6 IS NULL OR #OtherValue IS NULL)
BEGIN
<Statement1AsIs>
END
ELSE
BEGIN
<Statement2WithComputations>
END
Rewrite WHERE clause
On a short sidenote, I know you did not ask for it, but I have the feeling there must be a 'clearer' way to write these
((#Value1 IS NULL AND [value1_id] IS NULL) OR [value1_id] = ISNULL(#Value1, [value1_id]))
I've been playing around a bit and this is one way to say the same:
COALESCE(#Value1, [value1_id], 1001) = COALESCE([value1_id], 1001)
This is another one, equivalent and shorter:
#Value1 IS NULL OR [value1_id] = ISNULL(#Value1, [value1_id])
Check out this SQL Fiddle to see the equivalence.

Related

How to handle NULL values in WHERE clause and change target column based upon its encounter

I need the WHERE clause to change what column it is evaluating when NULL is encountered. For instance I'm trying to do something like this:
SELECT *
FROM customer c
WHERE CASE WHEN c.cust_id_1(#variable) IS NOT NULL THEN c.cust_id_1 = #variable
ELSE CASE WHEN c.cust_id_2(#variable) IS NOT NULL THEN c.cust_id_2 = #variable
ELSE c.cust_id_3 = #variable
Is something like this possible? One of the 3 cust_id's will not be NULL.
That seems like less than optimal table design, but isn't a simple COALESCE where you're after?
WHERE #variable = COALESCE(cust_id_1, cust_id_2, cust_id_3);
You don't need a CASE expression for this, you just need logical operators
SELECT *
FROM customer c
WHERE
(c.cust_id_1 IS NOT NULL AND c.cust_id_1 = #variable)
OR
(c.cust_id_2 IS NOT NULL AND c.cust_id_2 = #variable)
OR
(c.cust_id_3 IS NOT NULL AND c.cust_id_3 = #variable)

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 ''

SQL , SQL QUERIES

I have the following condition:
if column is NULL, or have value like Unassessed or ABC then the result should be I.
I need help how to put three in one condition like below. Just confused with NULL value. And it is string in column.
JobAssessStage ='Unassessed','','ABC'
JobAssessStage in ('Unassessed','','ABC')
Use below condition with is null
JobAssessStage in ('Unassessed','ABC') or JobAssessStage is null
use case when for multiple condition
select case when JobAssessStage in ('Unassessed','ABC') or
JobAssessStage is null then 'I' else 'You' End
from yourtable
In some cases you can use also use COALESCE function (it returns the first non-null expression in a list) on JobAssessStage field:
COALESCE(JobAssessStage, 'X') in ('Unassessed','ABC','X')
If JobAssessStage is NULL with COALESCE the value you're testing becomes X.
I told you "in some cases" because you have to be sure that the value you have chosen (X) is not a value that your field can take.
You can formulate your like below:
SELECT *
FROM table
WHERE 1 = 1
AND (JobAssessStage IN ('Unassessed', 'ABC')
OR JobAssessStage IS NULL);

Oracle no results, small query

I am having trouble with some sql. When I run the following query:
Select * from teleapp;
I get TONS of results. Resulst which include a column (called cashwithappyn) that has TONS of empty or null data cells. (They look empty and don't say null)
The column info is:
ColumnName ID Null? Data Type Histogram Num Distinct Num Nulls Density
CASHWITHAPPYN 54 Y VARCHAR2(1 Byte) Frequency 2 56895 0
When I try to run the following query:
Select * from teleapp where cashwithappyn = null;
or
Select * from teleapp where cashwithappyn = '';
or
Select * from teleapp where cashwithappyn not like '';
or
Select * from teleapp where cashwithappyn not in ('Y','N');
or ANY type of combination, I cannot seem to get all of the rows with nothing in cashwithappyn.
Any ideas? Please help, this is the last part of a project that I was assigned to do and I just need to figure this out.
Thanks.
Maybe the column contains blanks. In that case you can do
WHERE TRIM(CASHWITHAPYYN) IS NULL
TRIM removes all blanks before and after and if nothing is left anymore the value becomes NULL
e.g.
TRIM(' ') IS NULL -- one blank removed = true
TRIM(NULL) IS NULL -- true
Also NULL cannot be compared with = NULL but must be phrased IS NULL. NULL is not a value as such and that is why the comparison never works.
You need to use IS NULL
Select * from teleapp where cashwithappyn is null;
Logical test expressions (=, Not In, Like etc) with null result in a false so all of the following result in false
1 = NULL
1 <> NULL
NULL = NULL
NULL <> NULL
NULL NOT IN ('a','b')
NULL Not Like NULL
Additionally in oracle the zero length string is null so NOT LIKE '' will never return any rows
You'll need to use either is null, is not null or Coalesce
A co-worker and I did a bunch of research, here's what we came up with that will work. Any ideas why this works but not the others?
Select * from teleapp where nvl(cashwithappyn,'U') = 'U';

Visual Studio 2008 Case Expression using null

I'm trying to right a report that lets a user select either 'all values including null' or 'all values excluding null'. I placed a case expression in the where section. It doesn't work correctly for two reasons:
I can't pull the values of null and is not null at the same time.
In both cases it ends up pulling all values including null for the first one and only null values for the second.
Where
((CASE WHEN #nullvalue = 1 THEN check_rtn_void_dte end is not null) OR (CASE WHEN #nullvalue = 2 THEN check_rtn_void_dte end is null))
Is there a operator I can use that will pull null and is not null in one statement? Also, why is the first case pulling all data including null values?
Perhaps something like this would be simpler...
SELECT *
FROM MyTable
WHERE (#nullvalue = 2 OR check_rtn_void_dte IS NOT NULL)
That should pull out all results if #nullvalue = 2, or only non-NULL values if #nullvalue is something else.