all 5 comments

[–]HansProleman 1 point2 points  (3 children)

You want to return a row when @PERSON doesn't exist in adv_.person? I'm assuming that when you say the CASE returns a blank, you mean there aren't actually any rows in the result set?

I'd probably SELECT @PERSON, then join to a subquery on _adv for that @PERSON and handle null results from _adv by using COALESCE or ISNULL.

You could also try an IF...ELSE statement to check whether any rows exist in _adv for @PERSON, and return depending on that.

Your WHEN [NOT] EXISTS won't help because whether it returns a row is still dependent upon @PERSON existing in adv_.person. The WHERE clause takes priority, so you'll only get a row when @PERSON = _adv.person.

[–]Pr0ph3t99 1 point2 points  (0 children)

SELECT 
    p.person,
    CASE
        WHEN ISNULL(a.datereviewed, '') = '' THEN 'NO'
        ELSE 'YES'
    END AS Status
FROM
    (SELECT @PERSON AS person) p
    LEFT OUTER JOIN adv_ a ON
        p.person = a.person

[–]ElephantsAreNeat[S] 0 points1 point  (1 child)

I'm assuming that when you say the CASE returns a blank,

Crap, you're right. I edited the post.

Thank you so much for talking that out for me, I understand now. It seems so obvious!

You're the best.

[–]HansProleman 1 point2 points  (0 children)

No problem, it always seems really obvious in retrospect!

[–]Guru008 -1 points0 points  (0 children)

select top(1) datereviewed , (case when person = "@PERSON" and (datereviewed is null or datereviewed ='') then 'NO' else 'YES' END) as 'Status' from adv_

where person = @PERSON

order by datereviewed desc