Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:20am
Edited 10-Jan-13 17:19pm
v3
Comments
Sandeep Mewara at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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  

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



Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid