where clause work only when case is satisfied - sql

I have made a view that has a where clause :
WHERE M.Deleted = 0 AND I.Deleted = 0 AND L.Deleted = 0
is there a possible way to make L.Deleted = 0 only work on a specific case? My plan is when the M.TYPE_ID != 4 then add this L.Delete = 0 to where clause, and without it otherwise
I tried to make something like:
AND CASE WHEN M.TYPE_ID != 4 THEN L.DELETED = 0 ELSE --(i want here to make this entire part of where clause to be invalid if the case is not valid)
or there are other ways to do that like intersect the same select above but without that condition ?
any help would be appreciated, thanks.

Yes, you can do:
WHERE M.Deleted = 0 AND I.Deleted = 0 AND
(M.TYPE_ID = 4 OR L.Deleted = 0)
You don't need a case expression.
You might find the logic simpler to follow as:
WHERE M.Deleted = 0 AND I.Deleted = 0 AND
( NOT (M.TYPE_ID <> 4 AND L.Deleted <> 0) )

The simplest way would be to include the valid condition in two checks and or them together.
e.g.
Select * from x where (x.isvalid = 1 and x.deleted = 0) or (x.isvalid <> 1 and x.othercheck = 1)

Related

Can multiple 'OR' statements be combined if they have the same condition?

I have the below OR statement with each column = 1 as the condition. Is there a shorter way of doing this? I have tried HAVING and IN but neither worked.
(count(distinct case when (fiscal_mth_idnt_june21 = 1 or fiscal_mth_idnt_july21 = 1 or fiscal_mth_idnt_august21 = 1) then contact_key end)) / sum(fiscal_mth_idnt_may21)*100 as within_three_months
is the main code I am interested in.
The boolean expression:
fiscal_mth_idnt_june21 = 1 or fiscal_mth_idnt_july21 = 1 or fiscal_mth_idnt_august21 = 1
is equivalent to:
1 IN (fiscal_mth_idnt_june21, fiscal_mth_idnt_july21, fiscal_mth_idnt_august21)

Issue with case null in sql

SELECT Id
FROM dbo.OWL_AddlDataFields
WHERE CustomerId = #BaseCustomerId
AND AddlDataFieldCategoryId = #AddlDataFieldCategoryId
AND AddlDataFieldGroupId = CASE
WHEN #AddlDataFieldGroupId = 0 THEN NULL
ELSE #AddlDataFieldGroupId
END
AND Name = #DataFieldName
The above query does not returns any result. I think above query has issue with the 'AddlDataFieldCategoryId =' and the null from the case. Can anybody correct the query?
The issue with your CASE expression is that you are comparing a column to a predicate which might be NULL using the equals operator. This won't behave as expected, because NULL comparisons should use IS NULL instead of equals. One possible workaround would be to coalesce AddlDataFieldGroupId to a numeric value. Then, we can be certain that the CASE expression would only be comparing one number to another.
WHERE ... AND
COALESCE(AddlDataFieldGroupId, 0) = CASE WHEN
#AddlDataFieldGroupId = 0
THEN 0 ELSE #AddlDataFieldGroupId END
If your AddlDataFieldGroupId cannot be zero, then you don't need the CASE at all. This will do the job:
SELECT Id
FROM dbo.OWL_AddlDataFields
WHERE CustomerId = #BaseCustomerId
AND AddlDataFieldCategoryId = #AddlDataFieldCategoryId
AND AddlDataFieldGroupId = #AddlDataFieldGroupId
AND Name = #DataFieldName
If AddlDataFieldGroupId can be zero, but cannot be negative, you can do this:
SELECT Id
FROM dbo.OWL_AddlDataFields
WHERE CustomerId = #BaseCustomerId
AND AddlDataFieldCategoryId = #AddlDataFieldCategoryId
AND AddlDataFieldGroupId = CASE WHEN
#AddlDataFieldGroupId = 0 THEN -1 ELSE #AddlDataFieldGroupId END
AND Name = #DataFieldName
Alternatively, this will work regardless of what AddlDataFieldGroupId can be:
SELECT Id
FROM dbo.OWL_AddlDataFields
WHERE CustomerId = #BaseCustomerId
AND AddlDataFieldCategoryId = #AddlDataFieldCategoryId
AND AddlDataFieldGroupId = CASE WHEN
#AddlDataFieldGroupId = 0 THEN AddlDataFieldGroupId - 1 ELSE #AddlDataFieldGroupId END
AND Name = #DataFieldName
Note: all these solutions assume that AddlDataFieldGroupId cannot be NULL.

Changing when to if statement in SQL server

How do I change the following code to an if statement that returns a boolean 0 or 1 value? My end results I would like to have, is one column listing the interest rate of 2, and my results column with a 0 or 1 if the condition is true.
(Case when new_interestratevariability = 2
and (new_interestrateindex = 1 or new_interestrateindex = 2 or new_interestrateindex = 3 or new_interestrateindex = 4 or new_interestrateindex = 6)
and new_crms_dt = #Curr_Date
then 0 else 1 end) as CIEDIT_VAL_96,
Currently, I am getting something like below:
Results Table
To filter rows, use a Where clause. The Case statement in the Select clause will modify the value shown on the row.
Select *
from table
Where new_interestratevariability = 2
and new_interestrateindex IN (1,2,3,4,6)
and new_crms_dt = #Curr_Date
Found my answer, it was as simple as adding "not in" instead of just "in". Thanks everyone
(Case when new_interestratevariability = 2
and (new_interestrateindex not in(1,2,3,4,6))
and new_crms_dt = #Curr_Date
then 1 else 0 end) as CIEDIT_VAL_96,

Oracle SQL CASE expression in WHERE clause only when conditions are met

Cant quite figure this one out, i have a set of conditions that i want to be met only if a value is in a field.
So if the Status is complete i want to have three where clause's, if the status doesn't equal complete then i don't want any where clause.
Code
SELECT *
FROM mytable
WHERE CASE WHEN Status = 'Complete'
THEN (included = 0 OR excluded = 0 OR number IS NOT NULL)
ELSE *Do nothing*
It is usually simple to only use boolean expressions in the WHERE. So:
WHERE (Status <> 'Complete') OR
(included = 0 OR excluded = 0 OR number IS NOT NULL)
If Status could be NULL:
WHERE (Status <> 'Complete' OR Status IS NULL) OR
(included = 0 OR excluded = 0 OR number IS NOT NULL)
You can translate the natural language in SQL, then, if possible, reformulate.
SELECT *
FROM mytable
WHERE (Status = 'Complete' and (included = 0 OR excluded = 0 OR number IS NOT NULL))
or status <> 'Complete'
or status IS NULL;
It doesn't look like you really need a CASE statement, just use it like this:
SELECT *
FROM mytable
WHERE where (Status = 'Complete' and (included = 0 OR excluded = 0 OR number IS NOT NULL)) or (*Your do nothing*)

SQL - "IF" in Where Clause

I'm trying to write a stored procedure that will have 6 bit value flags as parameters and a couple of other values.
The pseudo sql I want to write is something like:
SELECT *
FROM theTable
WHERE
IF #flagA = 1 THEN theTable.A = 1
IF #flagB = 1 THEN theTable.B = 1
IF #flagC = 1 THEN theTable.CValue = #cValue
etc
Any ideas how I can do this in SQL or am I best reverting to building the SQL in C# (where this SP will be called from)?
SELECT *
FROM theTable
WHERE
(#flagA = 0 or (#flagA = 1 AND theTable.A = 1 ))
and (#flagB = 0 or (#flagB = 1 AND theTable.B = 1 ))
and (#flagC = 0 or (#flagC = 1 AND theTable.CValue = #cValue ))
Note: I am assuming your bit flags are non-nullable. If that is not the case, you will need to use ISNULL.