Case Statement on IN Clause

  • I am creating a query with case statement inside an in clause but I dont know if it is possible to specify multiple values. The following script is what I made so far, notice the (3,4) inside the case statement:

    select *

    from Delivery

    where VendorId

    in

    (

    case host_name()

    when 'HOSTNAME1' then 1

    when 'HOSTNAME2' then 2

    when 'HOSTNAME2' then (3,4)

    else 0

    end

    )

    Any help will be appreciated.

    "Often speak with code not with word,
    A simple solution for a simple question"

  • I suppose the values 3 and 4 should be associated with 'HOSTNAME3' rather than 'HOSTNAME2'.

    This is how I would do it:

    SELECT *

    FROM Delivery AS DLV

    WHERE EXISTS (

    SELECT *

    FROM (

    VALUES ('HOSTNAME1', 1),

    ('HOSTNAME2', 2),

    ('HOSTNAME3', 3),

    ('HOSTNAME3', 4)

    ) AS Hosts(name, vendor)

    WHERE Hosts.vendor = DLV.VendorId

    AND Hosts.name = HOST_NAME()

    )

    Hope this helps

    Gianluca

    -- Gianluca Sartori

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

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