Using AND operator

  • Hi all

    I'm trying to write a query that has a conditional AND clause. So, for example, I have a table that stores Job History information. I want to select all jobs that have a status not equal to 'Succeeded' (i.e. i want to know about all failed, cancelled and 'in progress' jobs). This is fine, but because we use log shipping i get hundreds of results returned due to the fact that the log shipping jobs are always 'in progress'.

    So really i want to know about all jobs with status not equal to succeeded AND also not equal to 'In Progress', but only execute the AND if the JobName is like 'LSAlert%' or 'LSRestore' (i.e. log shipping jobs). This is because i am still interested in knowing about jobs that are in progress, but just not if they are related to log shipping.

    Hoping you can help!

    Thanks

    Doodles 🙂

  • Think about what you don't want and then negate it.

    WHERE NOT (

    (

    Status = 'Succeeded'

    )

    OR (

    Status = 'In Progress'

    AND (JobName LIKE 'LSAlert%' OR JobName LIKE 'LSRestore%')

    )

    )

  • Thanks a lot - makes perfect sense now 🙂

    Doodles

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

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