Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to sql. I am in a situation where i have to put three different conditions in a single query. If any of the below conditions retreive records MessageBox should be shown "Records Retreived" else "Nothing Retreived".

Condition 1 :
C#
"Select Name, Age from Table1 where Class = '1st' and Date between #"+1st_Date.ToString() +"# and #"+2nd_Date.ToString() +"#" .


Condition 2 :
C#
"Select Name, Age from Table1 where Class = '2nd' and Date between #"+1st_Date.ToString() +"# and #"+2nd_Date.ToString() +"#"
.

Condition 3 :
C#
"Select Name, Age from Table1 where Class = '2nd' and Date > #"+1st_Date.ToString()+"# and Date  < #"+2nd_Date.ToString()+"#"


My question is how to write these three conditions in a single query. Hope you understand my question. I am searching for the answer for very long but all in vain. I will be very thankful of yours.
Posted
Updated 24-Jun-13 19:42pm
v3
Comments
sudipta biswas 22-Jun-13 1:51am    
You are trying to get data of 1st or 2nd class and between the two dates. Is that correct?

Use the OR operator:
SQL
Select Name, Age from Table1 where 
(Class = '1st' and Date between #"+1st_Date.ToString() +"# and #"+2nd_Date.ToString() +"# ) OR
(Class = '2nd' and Date between #"+1st_Date.ToString() +"# and #"+2nd_Date.ToString() +"# ) OR
(Class = '2nd' and Date > #"+1st_Date.ToString()+"# and Date < #"+2nd_Date.ToString()+"#)
 
Share this answer
 
SQL
SELECT
(CASE WHEN
EXISTS
(
SELECT condition 1
UNION ALL
SELECT condition 2
UNION ALL
SELECT condition 3
)
THEN
'Records retreived'
ELSE
'Nothing retreived'
END)
AS MessageBoxText
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900