Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

My Query IS

SQL
SELECT * FROM TableName WHERE Date Between Date1 AND Date2 AND ID=ISNULL(@ID,ID)


Here I have avoided dynamic query by using isnull in where condition.. Now i can search using or Date .. When Date is Not Passed the Date Between Date1 AND Date2 this have to pass... that is the case friends..

when date is null .. the condition should have to based on ID only. and between case should pass as true.. Hope U understand!!!!

Thanks,
SarathKumar.N
Posted
Updated 10-Jan-13 17:19pm
v3
Comments
Sandeep Mewara 10-Jan-13 8:53am    
If you mean date as empty == NULL then you can use DBNull for it. Not too clear on what are you trying to do.

Hi Friends,

Thank u for ur help.... And I have Solved this

SQL
SELECT * FROM tablename WHERE   Date BETWEEN  ISNULL(@Fromdate,Date) AND ISNULL(@ToDate,ISNULL(@Fromdate,Date))
             AND ID =ISNULL(@Id,ID)


This Query Worked Out For Me.. Thanks Friends for ur contribution....
 
Share this answer
 
You can pass null value to the procedure. please check the following thread:

http://stackoverflow.com/questions/1207404/how-to-pass-a-null-variable-to-a-sql-stored-procedure-from-c-net-code[^]
 
Share this answer
 
this trick will work
SQL
select * from a 
where 
(startdate between @t1 and @t2   or  (@t1 is null or @t2 is null)) 
and 
(id=@Id or id is null)

Happy Coding!
:)
 
Share this answer
 
If I understand your query then you have solved your requirement on your query. Just modifying your query only. Check it:

SELECT * FROM TableName WHERE IsNull(@Date,Date1) Between Date1 AND Date2 AND ID=ISNULL(@ID,ID)
 
Share this answer
 

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