How to apply the case when cluase together with paragraph

Here is my request. @ScoreCount

may contain zero or not. If it contains zero, it should be counted as zero, as well as null and null. How to do it in where where?

DECLARE @ScoreCount varchar(50)
SET @ScoreCount = '2,5,0'
SELECT e.name,e.id,e.degisnation 
FROM tblEmployee e INNER JOIN Department d ON d.id = e.did 
WHERE e.degisnation = @designation AND ScoreCount IN (@ScoreCount)

      

So I did it.

SELECT e.name,e.id,e.degisnation 
FROM tblEmployee e INNER JOIN Department d ON d.id = e.did 
WHERE e.degisnation =@designation AND
    CASE WHEN ScoreCount is null THEN 0 
         WHEN ScoreCount ='' THEN 0 
         ELSE ScoreCount END IN (2, 5, 0)

      

Does it fit correctly?

Here I made the database value null and '' applied first and later in the section. I have one request. How the execution is done. According to me

step 1 from --- > Result1
step2 apply join on Result1 -->Result2
step3 apply where clause first part on Result2 -->Result3
step4 apply case part on Result3 then msidn in part check -->Result4
setp 5 select Result4

      

Here it will first change the whole record of result3 to the corresponding msidn, then it will check the msidn in the clause, or one by one, change the record of the result3 to the corresponding msidn, then it will check the msidn in the section

+3


source to share


2 answers


You don't need case

a sentence where

. Just use traditional logic:

select e.name, e.id, e.degisnation
from tblEmployee e inner join
     Department d
     on d.id = e.did
where e.degisnation = @designation and
      (ScoreCount is null or ScoreCount = '' or ScoreCount in (2, 5) )

      



I removed 0

from the list. I guess he was only there for the first two conditions.

+2


source


I think if you just use:

...
    AND COALESCE(ScoreCount, 0) IN (0, 2, 5)
--or
-- AND ISNULL(ScoreCount, 0) IN (0, 2, 5)

      



Will handle them.

0


source







All Articles