how to use case and in clause together

  • COULD YOU PLEASE HELP ME? PLEASE NO DYNAMIC SQL

    CREATE TABLE STATEOFUSA

    (

    STATEUS CHAR(2)

    )

    INSERT INTO STATEOFUSA VALUES ('TX'),('CA'),('NY'),('FL'),('NM'),('AZ'),('AR')

    IF I SET @VALUE = 101, IT SHOULD DISPLAY ALL STATE WHICH TOUCH MAXICO COUNTRY MEAN -> WHERE STATEUS IN ('TX','CA','NM','AZ')

    OUTPUT SHOULD BE 'TX','CA','NM','AZ'

    IF I SET @VALUE = 102, IT SHOULD DISPLAY ALL STATE WHICH DO NOT TOUCH MAXICO COUNTRY MEAN -> WHERE STATEUS NOT IN ('TX','CA','NM','AZ')

    OUTPUT SHOULD BE 'NY','FL','AR'

    IF I SET @VALUE = 103, IT SHOULD DISPLAY ALL STATE (NO NEED WHERE CONDITION)

    OUTPUT SHOULD BE 'TX','CA','NY','FL','NM','AZ','AR'

    DECLARE @VALUE INT

    SET @VALUE = 101 --

    SELECT STATEUS from STATEOFUSA WHERE STATEUS in ..........

  • how to identify which states touch wich values?

  • I would add a column isAdjacent bit and instead of IS IN I would set the condition

    WHERE isAdjacent = 'true' or 'false' or omit the test.

  • do you have to use case ? In these query conditions you can use temp variables to do the comparisons..

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

  • yes I do have to use case

  • SELECT

    STATEUS

    from

    STATEOFUSA

    WHERE

    case

    when @VALUE = 103

    then 1

    when @VALUE in 101 and STATEUS in ('TX','CA','NM','AZ')

    then 1

    when @VALUE in 102 and STATEUS not in ('TX','CA','NM','AZ')

    then 1

    else 0 end = 1

  • Michael, I did try following way but still showing error before running. this is what I was looking for

    DECLARE @VALUE INT

    SET @VALUE = 101

    SELECT

    STATEUS

    from

    STATEOFUSA

    WHERE STATEUS =

    (case

    when @VALUE = 103

    then 1

    when @VALUE in 101 and STATEUS in ('TX','CA','NM','AZ')

    then 1

    when @VALUE in 102 and STATEUS not in ('TX','CA','NM','AZ')

    then 1

    else 0 end = 1)

  • based on the code above:

    DECLARE @VALUE INT

    SET @VALUE = 101

    SELECT

    *

    from

    Apprentices

    WHERE

    1 = case

    when @VALUE = 103

    then 1

    when @VALUE = 101 and id in ('TX','CA','NM','AZ')

    then 1

    when @VALUE = 102 and id not in ('TX','CA','NM','AZ')

    then 1

    else 0 end

  • Was this a homework assignment?

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • No it is not hw

  • Thank you it works now

  • Thanks michale it works now.

  • Munabhai (7/27/2012)


    Michael, I did try following way but still showing error before running. this is what I was looking for

    DECLARE @VALUE INT

    SET @VALUE = 101

    SELECT

    STATEUS

    from

    STATEOFUSA

    WHERE STATEUS =

    (case

    when @VALUE = 103

    then 1

    when @VALUE in 101 and STATEUS in ('TX','CA','NM','AZ')

    then 1

    when @VALUE in 102 and STATEUS not in ('TX','CA','NM','AZ')

    then 1

    else 0 end = 1)

    I really hate it when I post something that works, they change is to something else (like the obvious error above), and then complain they got an error.

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

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