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 |
helixpoint
Constraint Violating Yak Guru
291 Posts |
Posted - 2013-09-05 : 07:24:12
|
I have a start date and and end date in my table. I want to make sure the dates fall between those 2 date. The end date may be null which means there is no end date. How do I do this.DaveHelixpoint Web Developmenthttp://www.helixpoint.com |
|
helixpoint
Constraint Violating Yak Guru
291 Posts |
Posted - 2013-09-05 : 08:01:11
|
I am guessing...WHERE @date_to_check between rsf.StartDate and Coalesce(@date_to_check, NULL)DaveHelixpoint Web Developmenthttp://www.helixpoint.com |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-09-05 : 08:55:02
|
Couple of questions:1. When you say you want "dates to fall between those 2 dates", is that inclusive, or exclusive? In other words, if your start date is Sept 1, 2013 and end date is September 5, 2013, how many dates will qualify? 3,4, or 5?2. If you don't have an end date what should happen? Is any date that is greater (or greater than or equal to) the start date considered to meet the filter criteria?COALESCE(@date_to_check, NULL) does not seem to be correct. COALESCE function examines each parameter given to it and picks the first one that is not null. So in your case, if @date_to_check is not null, it will pick that. But if it is null, then it will move on to the next one, which happens to be NULL, so it will return null. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-09-05 : 12:58:45
|
It is good form to include how you want to handle the comparison. Perhaps sample data a expected output might help to show this sort of thing. But, given the lack of requirements, here is my guess:WHERE rsf.StartDate >= @date_to_check AND ( rsf.EndDate <= @date_to_check OR rsf.EndDate IS NULL )-- OrWHERE @date_to_check BETWEEN rsf.StartDate AND COALESCE(rsf.EndDate, CAST('9999-12-31' AS DATE)) |
|
|
|
|
|
|
|