Click here to Skip to main content
15,890,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hai all

is there any SQL query for checking
whether a date between two dates falls between other two dates .
the database in sql server 2008,from_date and close_date are in date format.
that is, for example, i want to know which of the following range has dates in between '2004-04-01' and '2005-02-01'
and the range has dates in between '2002-04-01' and '2013-04-01'

from_date close_date
2003-04-01 2006-03-31
2003-04-01 2005-03-31
2003-04-01 2005-03-31
2003-04-01 2009-09-30
2003-04-01 2005-03-31
2003-04-01 2006-03-31
2003-04-01 2012-03-31
2003-04-01 2006-03-31
2003-04-01 2012-03-31
2003-04-01 2006-03-31
2003-04-01 2012-03-31
2003-04-01 2006-03-31
2003-04-01 2006-03-31
2003-04-01 2006-03-31
2003-04-01 2006-03-31
2003-04-01 2006-03-31
2003-04-01 2009-03-31
2003-04-01 2007-03-31
2003-04-01 2006-03-31
2003-04-01 2004-09-30
2003-04-01 2004-03-31


can any one help
thanks in advance
Posted
Updated 1-May-12 22:40pm
v3

to get the date between Specified Dates:
SQL
Select * From MyTable Where timecreated Between Cast('7/20/08 12:01:01' As DateTime) And Cast('7/20/09 12:01:01' as DateTime)
 
Share this answer
 
Completely or partially overlapping?
For a complete overlap, the WHERE clause is
SQL
WHERE from_date<@min_date AND close_date>@max_date

For a partial overlap:
SQL
WHERE from_date<@max_date AND close_date>@min_date
 
Share this answer
 
Comments
VJ Reddy 2-May-12 3:54am    
Good answer. 5!
I think for the partial overlap OR is required instead of AND.
WHERE ((m.[from_date] <= @min_date and m.[close_date] >= @max_date )OR(m.[from_date]>= @min_date and m.[from_date]< @max_date ) OR(m.[close_date] <= @max_date and m.close_date > @min_date ) OR((m.[from_date] >= @min_date ) and m.[from_date] < @max_date ) and ((m.[close_date] <= @max_date )and m.[close_date] > @min_date ))
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900