CASE Statement in WHERE CLAUSE

  • I'm trying to do the following in SQL 2008

    .....

    where

    if @openorder>1 then o.openorder=1 or o.openorder =0

    else

    o.openorder=@openorder

    endif

    Can this be done? if so how can I do it

  • Crap code too!!!!!!!!!!:w00t:

  • Like this:

    SELECT openorder

    FROM @Table

    WHERE

    openorder =

    ( CASE WHEN @openorder > 1 THEN 1

    ELSE @openorder

    END )

    OR

    openorder =

    ( CASE WHEN @openorder > 1 THEN 0

    ELSE @openorder

    END )

  • ColdCoffee (5/6/2011)


    Like this:

    {CRAP CODE}

    DECLARE @openorder INT

    SET @openorder = 2

    DECLARE @tbl TABLE (openorder INT)

    INSERT INTO @tbl (openorder)

    SELECT 0

    UNION ALL

    SELECT 1

    UNION ALL

    SELECT 2

    --UNION ALL

    SELECT * FROM @tbl o

    WHERE

    CASE WHEN @openorder > 1 THEN o.openorder IN ( 0 ,1 )

    ELSE o.openorder=@openorder

    END

    Msg 156, Level 15, State 1, Line 21

    Incorrect syntax near the keyword 'IN'.

  • DECLARE @openorder INT

    SET @openorder = 2

    DECLARE @tbl TABLE (openorder INT)

    INSERT INTO @tbl (openorder)

    SELECT 0

    UNION ALL

    SELECT 1

    UNION ALL

    SELECT 2

    UNION ALL

    SELECT 3

    UNION ALL

    SELECT 4

    --UNION ALL

    SELECT

    *

    FROM

    @tbl o

    WHERE

    1 = CASE WHEN @openorder > 1

    AND o.openorder IN ( 0 , 1 ) THEN 1

    ELSE --0

    CASE WHEN o.openorder = @openorder THEN 1

    ELSE 0

    END

    END

  • Ninja's_RGR'us (5/6/2011)


    ColdCoffee (5/6/2011)


    Like this:

    {CRAP CODE}

    Msg 156, Level 15, State 1, Line 21

    Incorrect syntax near the keyword 'IN'.

    Fixed the code 🙂

  • Ninja's_RGR'us (5/6/2011)


    Crap code too!!!!!!!!!!:w00t:

    😛

  • ColdCoffee (5/6/2011)


    Ninja's_RGR'us (5/6/2011)


    Crap code too!!!!!!!!!!:w00t:

    😛

    ya but you beat me to it :w00t:

    So somehow it must be your fault :hehe:

  • Thanks guys - this works

    1 = CASE WHEN @openorder > 1 AND o.openorder IN ( 0 , 1 ) THEN 1

    ELSE --0

    CASE WHEN o.openorder = @openorder THEN 1

    ELSE 0

    END END

  • Ninja's_RGR'us (5/6/2011)


    ColdCoffee (5/6/2011)


    Ninja's_RGR'us (5/6/2011)


    Crap code too!!!!!!!!!!:w00t:

    😛

    ya but you beat me to it :w00t:

    So somehow it must be your fault :hehe:

    Surrendering, Master :w00t: 😛

  • Alright... let's just call it friday afternoon and time to go home.

    l8r! ;-):-P:-D:hehe::smooooth:

  • Ninja's_RGR'us (5/6/2011)


    Alright... let's just call it friday afternoon and time to go home.

    l8r! ;-):-P:-D:hehe::smooooth:

    Friday afternoon, and going HOME ?? :w00t::-D

  • ColdCoffee (5/6/2011)


    Ninja's_RGR'us (5/6/2011)


    Alright... let's just call it friday afternoon and time to go home.

    l8r! ;-):-P:-D:hehe::smooooth:

    Friday afternoon, and going HOME ?? :w00t::-D

    It's already 3:30 PM here. Was about time I got home!

  • So, did BIGC2269 get what he needed? BIGC2269, what was the issue?

    I found this worked.

    DECLARE @openorder INT

    SET @openorder = 2

    DECLARE @Table TABLE (openorder INT)

    INSERT INTO @Table (openorder)

    SELECT 0

    UNION ALL

    SELECT 1

    UNION ALL

    SELECT 2

    UNION ALL

    SELECT 3

    UNION ALL

    SELECT 4

    --UNION ALL

    SELECT openorder

    FROM @Table

    WHERE

    openorder =

    ( CASE WHEN @openorder > 1 THEN 1

    ELSE @openorder

    END )

    OR

    openorder =

    ( CASE WHEN @openorder > 1 THEN 0

    ELSE @openorder

    END )

    This did not work.

    DECLARE @openorder INT

    SET @openorder = 2

    DECLARE @tbl TABLE (openorder INT)

    INSERT INTO @tbl (openorder)

    SELECT 0

    UNION ALL

    SELECT 1

    UNION ALL

    SELECT 2

    UNION ALL

    SELECT 3

    UNION ALL

    SELECT 4

    --UNION ALL

    SELECT

    *

    FROM

    @tbl o

    WHERE

    1 = CASE WHEN @openorder > 1

    AND o.openorder IN ( 0 , 1 ) THEN 1

    ELSE --0

    CASE WHEN o.openorder = @openorder THEN 1

    ELSE 0

    END

    END

    --Quote me

Viewing 14 posts - 1 through 13 (of 13 total)

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