Case statement

  • This case statement works without options 1 and 2.

    DECLARE @w int

    DECLARE @C varchar(30)

    DECLARE @v-2 varchar(30)

    SET @w=3

    SET @C='Mr'

    SET @v-2='Mr'

    SELECT m.MRmanMemberID,m.MRmanTitle,MRmanIsFinancial

    FROM MRman_Main m

    where case

    --when @w=1 then MRmanMemberID

    --when @w=2 then MRmanIsFinancial

    when @w=3 then MRmanTitle end

    between @C and @v-2

    --change it to

    SELECT m.MRmanMemberID,m.MRmanTitle,MRmanIsFinancial

    FROM MRman_Main m

    where case

    when @w=1 then MRmanMemberID

    when @w=2 then MRmanIsFinancial

    when @w=3 then MRmanTitle end

    between @C and @v-2

    --and get the error message:

    Server: Msg 245, Level 16, State 1, Line 9

    Syntax error converting the varchar value 'Mr' to a column of data type int.

    Anybody have any ideas???

    Also:

    if you change @w to 1 and @C to 1 and @w to 20 that works.

    MRmanMemberID is an int field and MRmanIsFinancial is a bit field and MRmanTitle is a varchar(12) field.

    Bruce - Coffs Harbour

  • Try this

    CAST((case

    when @w=1 then MRmanMemberID

    when @w=2 then MRmanIsFinancial

    when @w=3 then MRmanTitle end) AS VARCHAR(30))

    The problem is Mr is a varchar and auto casting does not generally take place (I have seen exceptions) but the Int column and bit column are non-comparable types. This should work.

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

    Edited by - antares686 on 03/19/2002 6:40:04 PM

  • Thanks but it didn't work. I see the problem though.

    It's OK if I cast the individual fields; but that doesn't work for other queries. I have something to work with now. 🙂

    Bruce - Coffs Harbour

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

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