473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Following is the Code I am trying

Expand|Select|Wrap|Line Numbers
  1. Declare @Day Int
  2. Set @Day=1
  3. Select 
  4.     Mobile,
  5.     Contact As Name,
  6.     InTimeDay1
  7. From uvw_UserDetails
  8. Where 
  9.     Case 
  10.         When @Day=1 Then Day1=1
  11.         When @Day=2 Then Day2=1
  12.         When @Day=3 Then Day3=1
  13.         When @Day=4 Then Day4=1
  14.         When @Day=5 Then Day5=1
  15.     End
  16.  
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?
Dec 11 '10 #1
2 2837
ck9663
2,878 Expert 2GB
Yes you can...It's called conditional where clause...

Expand|Select|Wrap|Line Numbers
  1. WHERE 
  2. @variable =
  3. case 
  4. when @variable = 1 then column1
  5. when @variable = 2 then column2
  6. else -1
  7. end
  8.  
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
Dec 12 '10 #2
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.
Dec 12 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Largo SQL Tools | last post by:
Can anyone tell me if it's possible to use a Case statement in a Where clause, and if so, the proper syntax? J.R. Largo SQL Tools The Finest Collection of SQL Tools Available...
2
by: David Smithz | last post by:
Hi, If you run a query which has a WHERE statement in which has a few possibilities separated OR statements, e.g. Select * from table where (Afield = 2) OR (Bfield = 2) OR (Cfield = 2) In...
10
by: John | last post by:
Hi I have a form with tow fields for dates in dd/mm/yyyy format. I am trying to use the fields in a query's where clause as below; "SELECT * " & _ "FROM Orders " & _ " WHERE Orders.)>= #" &...
9
by: paulmac106 | last post by:
If you could help me with my syntax i would really appreciate it. I'm trying to change the where clause on the fly, but it's giving me a syntax error. Hopefully from what I have below you can...
2
sanjay123456
by: sanjay123456 | last post by:
Dear friends, see the Following query ----- SELECT headline, Concat(EXTRACT(YEAR FROM datetime),'-',EXTRACT(MONTH FROM datetime),'-',DAYOFMONTH(datetime)) as abc FROM news_news where...
2
by: vinayakkale2 | last post by:
Hi I am using a select query from front end to generate report in such a way that I should be able to get records of Table W which are present in Table Y The query which i am using is as follows: ...
6
by: Jared | last post by:
Consider the following two functionally identical example queries: Query 1: DECLARE @Name VARCHAR(32) SET @Name = 'Bob' SELECT * FROM Employees WHERE = CASE WHEN @Name IS NULL THEN ELSE...
6
by: Plissskin | last post by:
I need to create an "ad-hoc" filtering page in a web app we are building. The page would display a number of drop down lists, text boxes, and radio lists and allow the user to select (enter) some...
10
by: Qtip23 | last post by:
Hello All, I am unable to get my VBA code to show all the records and allow me to view the filtered responses from user input, once my cmd_click() event fires. The form displays the 19...
3
by: JenniferM | last post by:
Back again with a brand new problem. My head hurts.... I've got a couple of listboxes on a form, FrmPatientDemographics. One contains knee surgeries for that patient (LbxKneeSx) and the other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.