Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
veebster
Starting Member
3 Posts |
Posted - 2005-01-24 : 20:47:58
|
| I have a table of data that has two date fields amongst othersStartDateEndDateUsually they contain a null value but if one of them contains a date, how can I NOT include that particular record in a query if:The start date is before the current date or the end data is after the current dateMany thanks |
|
|
jhermiz
3564 Posts |
Posted - 2005-01-24 : 21:34:25
|
quote: Originally posted by veebster I have a table of data that has two date fields amongst othersStartDateEndDateUsually they contain a null value but if one of them contains a date, how can I NOT include that particular record in a query if:The start date is before the current date or the end data is after the current dateMany thanks
WHERE (@StartDate > GetDate() --negates your condition OR @EndDate < GetDate()) --negates your second conditionTaking the NOT of all that you get@StartDate < GetDate() (start date is before the current date)OR (your or condition) becomes AND@EndDate > GetDate() (end date is after the current date)So basically I just took what you said and negated it. Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]Imperfection living for perfection -- [url]http://jhermiz.blogspot.com/[/url] |
 |
|
|
jhermiz
3564 Posts |
Posted - 2005-01-24 : 21:36:51
|
Forgot to mention in your sproc make the parameters take NULLS as defaults:@Startdate datetime=NULL,@Enddate datetime=NULLThen you can use boolean logic to see if a value exists:YourTable.YourField=@StartDate OR @StartDate IS NULL1 OR 1 = 11 OR 0 = 10 OR 1 = 10 or 0 = 0In your case if you pass null to the start date the second portion (@StartDate IS NULL) holds true, else it has a date and the first portion YourTable.YourField=@StartDate holds true.HTH Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]Imperfection living for perfection -- [url]http://jhermiz.blogspot.com/[/url] |
 |
|
|
|
|
|