Sql Server Where Case Then Is Null Else Is Not Null
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 row开发者_如何学Gos 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 beNULL
.
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
精彩评论