SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 null date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

helixpoint
Constraint Violating Yak Guru

278 Posts

Posted - 09/05/2013 :  07:24:12  Show Profile  Reply with Quote
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.

Dave
Helixpoint Web Development
http://www.helixpoint.com

helixpoint
Constraint Violating Yak Guru

278 Posts

Posted - 09/05/2013 :  08:01:11  Show Profile  Reply with Quote
I am guessing...

WHERE @date_to_check between rsf.StartDate and Coalesce(@date_to_check, NULL)

Dave
Helixpoint Web Development
http://www.helixpoint.com
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3697 Posts

Posted - 09/05/2013 :  08:55:02  Show Profile  Reply with Quote
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.

Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 09/05/2013 :  12:58:45  Show Profile  Reply with Quote
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
	)

-- Or

WHERE 
	@date_to_check BETWEEN rsf.StartDate AND COALESCE(rsf.EndDate, CAST('9999-12-31' AS DATE))
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000