Search code examples
sqlsql-server-2012where-clausewhere-in

Including a conditional sql where statement


I'm trying to include a conditional where statement in my SQL code and i'm not sure exactly how to do it... currently it looks like this (see below) although i'm fairly certain that i'm going the wrong direction here.

     WHERE 
     [awb_prefix] in ('403','369')
     AND [agent_iata_code] NOT IN ('0508634')
     AND [yr] = 2014
     AND CASE WHEN [awb_prefix] = '369' THEN [origin_gw_carrier] in ('PO','9S','K4') ELSE [origin_gw_carrier] in ('PO','9S','K4','5Y')
     END

Solution

  • That isn't what CASE is for. You can try doing it like this:

    WHERE   [awb_prefix] in ('403','369')
    AND     [agent_iata_code] NOT IN ('0508634')
    AND     [yr] = 2014
    AND 
    (
        ([awb_prefix] = '369' AND [origin_gw_carrier] in ('PO','9S','K4'))
    OR  ([awb_prefix] <> '369' AND [origin_gw_carrier] in ('PO','9S','K4','5Y'))
    )