Is it possible to use Case in Where clause as follows?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Napsterr
    New Member
    • Sep 2010
    • 14

    Is it possible to use Case in Where clause as follows?

    Following is the Code I am trying

    Code:
    Declare @Day Int
    Set @Day=1
    Select 
    	Mobile,
    	Contact As Name,
    	InTimeDay1
    From uvw_UserDetails
    Where 
    	Case 
    		When @Day=1 Then Day1=1
    		When @Day=2 Then Day2=1
    		When @Day=3 Then Day3=1
    		When @Day=4 Then Day4=1
    		When @Day=5 Then Day5=1
    	End
    In all total I want to use different Columns in where clause depending upon the value I pass to the variable.
    Can it be done with Case Statement?
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Yes you can...It's called conditional where clause...

    Code:
    WHERE 
    @variable =
    case 
    when @variable = 1 then column1
    when @variable = 2 then column2
    else -1
    end
    This is just simulation. If you can post some sample data and your required results, we might be able to give more efficient help.

    Good Luck!!!


    ~~ CK

    Comment

    • Napsterr
      New Member
      • Sep 2010
      • 14

      #3
      I have table Say tblEventDays
      Which contains Columns
      VisitorId BigInt
      Day1 Bit
      Day2 Bit
      Day3 Bit
      Day4 Bit
      Day5 Bit
      IntimeDay1 Nvarchar
      IntimeDay2 Nvarchar
      IntimeDay3 Nvarchar
      IntimeDay4 Nvarchar
      IntimeDay5 Nvarchar

      Whenever any Visitor enters in the event his entry for that day goes into this Table.
      e.g.
      "123456" comes to the event for first day then Day1=True/1
      IntimeDay1="11: 30 AM"

      I want to generate a report.When User Selects Day from drpDayDownList. I want to show him those Visitors who came on Specific Day.
      My event has only 5 days.
      So I was trying to write the query I specified at first.
      So, to conclude this I want to Compare different columns in Where Clause depending upon variable value to the procedure.
      e.g.
      When I will pass @Day as 1 Then Query should compare for column Day1=1 and so on.
      Can you help?
      Thank You.

      Comment

      Working...