Help adding an if statement around a nested query

  • Current Query:

    Select person_id, (Select 'Y' From notes where note_date > '06/23/2008' AND note_date > '06/29/2008') From Persons

    Attempted Query:

    Select person_id, IF((Select count(*) From notes where note_date > '06/23/2008' AND note_date 0 BEGIN Select 'Y' END ELSE BEGIN SELECT 'N' END From Persons

    This is not my actual query but it has the similar structure. I know theres easier ways to do this particular query like using ISNULL(, '') but that would work with my real query as it will run 1 query if its true and a totally different query if its false.

    Is it possible to use if statements like this? Do I have a syntax issue?

  • Try the following way...

    SELECT @cnt = Count(*) From notes where note_date > '06/23/2008'

    Select person_id, case @cnt when 0 Then 'Y' ELSE 'N' END From Persons

    The solution might be different IF you need the Count(*) row wise on person table.

    Atif Sheikh

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply