replace nested IF..Else in query using CASE

  • below is the nested IF ELSE statement i am using it in my store procedure ,,but now i want to replace that code using the case statement so i can use it in select statement.

    Is there any way that we can achive it using CASE??

    if (column1 IS NOT NULL and column2 IS NOT NULL and (column3 IS NULL or column3 = 0))

    begin

    set column3 = 1

    end

    else begin

    if (column1 IS NOT NULL and (column2 IS NULL or column2= 0) and column3 IS NOT NULL)

    begin

    set column2= 1

    end

    else begin

    if ((column1 IS NULL or column1 = 0) and column2 IS NOT NULL and column3 IS NOT NULL)

    begin

    set column1 = 1

    end

    else begin

    if (column1 IS NOT NULL and (column2 IS NULL or column2= 0) and (column3 IS NULL or column3 = 0))

    begin

    set column3 = 1

    set column2= 1

    end

    else begin

    if ((column1 IS NULL or column1 = 0) and column2 IS NOT NULL and (column3 IS NULL or column3 = 0))

    begin

    set column1 = 1

    set column3 = 1

    end

    else begin

    if ((column1 IS NULL or column1 = 0) and (column2 IS NULL or column2= 0) and column3 IS NOT NULL)

    begin

    set column1 = 1

    set column2= 1

    end

    end

    end

    end

    end

    end

  • Case

    When col1 is not null and col2 is not null then col4=1

    when col1 is not null and col2 is null then col4=2

    end

    Dan

    If only I could snap my figures and have all the correct indexes apear and the buffer clean and.... Start day dream here.

  • Thanks for your quick response.

    But in CASE statement we can't write

    then column4=1

    thats not accepted in case statement.

  • I think you just need to remove "col4=".

    John

  • Thanks for all the response.

    Finally I figured out. I have to write there case statement for 3 column.

Viewing 5 posts - 1 through 4 (of 4 total)

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