use Case statement to simplify WHERE clause of Select query

  • Hi,

    I want to use Case statement to simplify WHERE clause of Select query. In other words, I want to use Case to combine 2 separate Select statements into 1 Select statement only and do not need to use IF… ELSE.

    Details

    I have a table “SchHtmlControl”

    SchHtmlControlID Control Description

    ---------------- -------------------------------------------------- --------------------------------------------------

    1 DropDownList input data using DropDownList

    2 TextBox input data using TextBox

    3 RadioButton option

    My query batch:

    declare @optionSchHtmlControl int

    /* if @optionSchHtmlControl =1 then pick DropDownList from the table;

    @optionSchHtmlControl = 2 then pick TextBox

    @optionSchHtmlControl = 1000 then pick DropDownList and TextBox

    */

    set @optionSchHtmlControl = 1

    IF(@optionSchHtmlControl = 1000)

    BEGIN

    Select * from SchHtmlControl where SchHtmlControlID in (1,2)

    END

    ELSE

    BEGIN

    Select * from SchHtmlControl where SchHtmlControlID = @optionSchHtmlControl

    END

  • SELECT *

    FROM SchHtmlControl

    WHERE (@optionSchHtmlControl = 1000 And SchHtmlControlID in (1,2) )

    Or       SchHtmlControlID = @optionSchHtmlControl

  • PW,

    Good helps. Thanks so much.

    BTW, OR operator can be used at places where CASE cannot be used in WHERE clause?

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

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