Click here to Skip to main content
Sign Up to vote bad
good
See more: C#SQL-Server
Hi All,
 
My Query IS
 
   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 10 Jan '13 - 2:20
Edited 10 Jan '13 - 17:19

Comments
Sandeep Mewara - 10 Jan '13 - 8:53
If you mean date as empty == NULL then you can use DBNull for it. Not too clear on what are you trying to do.

4 solutions

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[^]
  Permalink  
this trick will work
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!
Smile | :)
  Permalink  
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)
  Permalink  
Hi Friends,
 
Thank u for ur help.... And I have Solved this
 
 
 
 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....
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 414
1 Arun Vasu 223
2 OriginalGriff 190
3 CPallini 163
4 Aarti Meswania 158
0 Sergey Alexandrovich Kryukov 10,169
1 OriginalGriff 7,749
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid