Sql Server Where Case Then Is Null Else Is Not Null - sql

I have a procedure which receive a bit variable called #FL_FINALIZADA.
If it is null or false I want to restrict my select to show only the rows that contain null DT_FINALIZACAO values. Otherwise I want to show the rows containing not null DT_FINALIZACAO values.
Something like this:
SELECT
*
FROM
MyTable
WHERE
...
AND
(
OPE.DT_FINALIZACAO = (
CASE
WHEN (#FL_FINALIZADA <> 1)
THEN NULL
END
) OR
OPE.DT_FINALIZACAO IS NOT NULL
)
In this case I receive the message:
None of the result expressions in a
CASE specification can be NULL.
How can I achieve this?
Thanks in advance.

SELECT
*
FROM
MyTable
WHERE
(ISNULL(#FL_FINALIZADA, 0) = 0
AND
OPE.DT_FINALIZACAO IS NULL
)
OR
(#FL_FINALIZADA = 1
AND
OPE.DT_FINALIZACAO IS NOT NULL
)

Change the AND to be:
AND (((#FL_FINALIZADA <> 1) AND (OPE.DT_FINALIZACAO IS NULL)) OR ( (#FL_FINALIZADA = 1) AND (OPE.DT_FINALIZACAO IS NOT NULL)))
If the bit flag is 1 then DT_FINALIZACAO can't be null.

IF #FL_FINALIZADA IS NULL
SET #FL_FINALIZADA = 0
SELECT * FROM NewsletterSubscribers
WHERE
(#FL_FINALIZADA = 0 AND OPE.DT_FINALIZACAO IS NULL)
OR
(#FL_FINALIZADA = 1 AND OPE.DT_FINALIZACAO IS NOT NULL)

My detailed SQL is a little rusty, but have you tried using 0 insted of NULL? I would expect 0 to evaluate the same as NULL in that select

Related

Range of values as ELSE clause in a CASE statement

I have a stored proc which is supposed to filter results based on an integer which can be NULL and I need to be able to choose results that either match the integer or retrieve all results but I can't figure out the syntax for the "All" results part. Query is basically this:
Select * from dbo.TABLE
WHERE [IDField] =
CASE
WHEN ISNULL(#ID, 0) > 0 THEN #ID
ELSE ?????
END
I want to use an IN range to include all the values because setting it to 0 or Null will return no results, but this doesn't work:
Select * from dbo.TABLE
WHERE [IDField] =
CASE
WHEN ISNULL(#ID, 0) > 0 THEN #ID
ELSE 1 OR 2 OR 3
END
or this:
Select * from dbo.TABLE
WHERE [IDField] IN (
CASE
WHEN ISNULL(#mID, 0) > 0 THEN #mID
ELSE 1,2,3
END
)
Phrase this without the case:
SELECT *
FROM dbo.TABLE
WHERE #id is null OR IDField = #id;
Then you can expand this to:
SELECT *
FROM dbo.TABLE
WHERE (COALESCE(#ID, 0) > 0 AND IDField = #id) OR
IDField IN (1, 2, 3)

Choose multiple cases condition

I want to get multipule choises after then in case statment as
#value
select * from [dbo].[Currency_Tbl]
WHERE [Currency_Active_YN]=
CASE WHEN #value = 1 THEN
( 1 or 0)
ELSE
#Value = 0 then 0
END
it didn't accept the first line in col1 but accept the col2
how can I select multiple numbers after THEN?
You don't use case in where clauses. Use boolean logic
select * from [dbo].[Currency_Tbl]
WHERE (#value = 1 and [Currency_Active_YN] in (0,1))
OR (#value = 0 and [Currency_Active_YN] = 0)
You dont need a case to do what you're trying to do. Assuming Currency_Active_YN is a not null bit field the following logic should suffice.
select * from [dbo].[Currency_Tbl]
WHERE (#value=1 OR [Currency_Active_YN]=#Value)

How to combine two nullable boolean fields in SQL

I have these fields :
ISNULL(
dbo.Features.TheifAlarm,
0
) AS ExtraTheifAlarm,
ISNULL(
dbo.Features.FireAlarm,
0
) AS ExtraFireAlarm,
dbo.Features.TheifAlarm and dbo.Features.FireAlarm are nullable bits. But I'm using EntityFramework and they are nullable boolean in my model. I want to combine them with OR.
This is my attempt :
ISNULL(
dbo.Features.TheifAlarm,
0
)
OR
ISNULL(
dbo.Features.FireAlarm,
0
)
But it didn't work, how can I combine these two columns? Thanks in advance
To 'combine' two fields you can use bitwise operators.
In your case (in the SELECT list):
SELECT (ISNULL(col1, 0) | ISNULL(col2, 0)) AS combinedCol FROM ...
Or as a WHERE condition:
SELECT ... WHERE (ISNULL(col1, 0) = 1 OR ISNULL(col2, 0) = 1)
Actually you don't even need the ISNULL() in the WHERE condition (col1 = 1 evaluates to FALSE (ok, NULL, but treated as FALSE) when col1 is null):
SELECT ... WHERE (col1 = 1 OR col2 = 1)
Edit: Just to add some alternatives:
Simple addition
SELECT IIF((ISNULL(col1, 0) + ISNULL(col2, 0)) > 0, 1, 0)
CASE..WHEN structure (as iamdave mentioned)
SELECT CASE WHEN col1 = 1 THEN 1 ELSE IIF(col2 = 1, 1, 0) END
Another CASE..WHEN
SELECT CASE WHEN (col1 = 1 OR col2 = 1) THEN 1 ELSE 0 END
or with IIF
SELECT IIF(col1 = 1 OR col2 = 1), 1, 0)
I am assuming you want to return a true if either field is true?
If so, you can check for both with a case:
select case when ISNULL(dbo.Features.TheifAlarm,0) = 0 -- If the first is False
then ISNULL(dbo.Features.FireAlarm,0) -- Return the value of the second
else 1 -- Otherwise return True
end as EitherAlarmCheck
If you are looking to filter your dataset for one of these values being True, you can use an OR in your where clause:
select Cols
from Table
where ISNULL(dbo.Features.TheifAlarm,0) = 1
or ISNULL(dbo.Features.FireAlarm,0) = 1

SQL Server: WHERE clause with null comparison

I have SP that has a parameter #NumeroTransferencia int.
I need to select rows that match that variable when it is not null.
But when it's null, I have to return the ones which have H.CodigoTransferencia = null
I tried the following but I didn't get a good result:
SELECT
*
FROM RUTEO.HOJADERUTA H
WHERE
(H.CodigoTransferencia IS NULL OR H.CodigoTransferencia = #NumeroTransferencia)
Thanks!
SELECT
*
FROM RUTEO.HOJADERUTA H
WHERE
((H.CodigoTransferencia IS NULL AND #NumeroTransferencia IS NULL) OR H.CodigoTransferencia = #NumeroTransferencia)
You can try this
SELECT
*
FROM RUTEO.HOJADERUTA H
WHERE
(#NumeroTransferencia IS NULL
AND H.CodigoTransferencia IS NULL)
OR (#NumeroTransferencia IS NOT NULL
AND H.CodigoTransferencia = #NumeroTransferencia)
SELECT *
FROM RUTEO.HOJADERUTA H
WHERE (
#NumeroTransferencia IS NULL
AND H.CodigoTransferencia IS NULL
)
OR
(
#NumeroTransferencia IS NOT NULL
AND H.CodigoTransferencia = #NumeroTransferencia
)
SELECT
*
FROM RUTEO.HOJADERUTA H
WHERE
((#NumeroTransferencia IS NULL OR H.CodigoTransferencia = #NumeroTransferencia) H.CodigoTransferencia IS NULL)
This will only perform the search if your variable is not null otherwise it will look for H.CodigoTransferencia is equal to your variable otherwise where H.CodigoTransferencia = null

conditional 'and' in where clause depending existing value?

Can anybody knows if I could use a "conditional 'and' in where clause" depending of an existing value??, I have a query like this:
#declare idProduct int = 1,
#declare idProducType int = null
select
tbl.idProduct, tbl.idProductType, tbl.ProductName, tbl.ProductPrice
from MyTable tbl
where tbl.idProduct = 1
<IF idProductType is not null>
and tbl.idProductType = SOMEVALUE
<ELSE>
do nothing
<ENDIF>
I require get data from this query always instead of getting null values when condition is false. how I can do like this in SQL Server 2008 ???
I think this is what you want:
select
tbl.idProduct, tbl.idProductType, tbl.ProductName, tbl.ProductPrice
from MyTable tbl
where tbl.idProduct = 1 and (tbl.idProductType = #idProducType or #idProducType is null)
This should work for you:
#declare idProduct int = 1,
#declare idProducType int = null
SELECT
tbl.idProduct, tbl.idProductType, tbl.ProductName, tbl.ProductPrice
FROM MyTable tbl
WHERE tbl.idProduct = 1
AND (CASE WHEN idProductType is not null THEN tbl.idProductType = SOMEVALUE
END)
....
where tbl.idProduct = 1
and (CASE WHEN idProductType is not null and tbl.idProductType = SOMEVALUE Then <something> END) = <something>
/