Sql 检查给定日期是否介于一系列日期之间
如果表中有两个日期列,startDate和endDate。如何返回给定日期介于这两个日期之间的行?例如: 如果给定日期为2012年10月25日 它应该返回以下行Sql 检查给定日期是否介于一系列日期之间,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,如果表中有两个日期列,startDate和endDate。如何返回给定日期介于这两个日期之间的行?例如: 如果给定日期为2012年10月25日 它应该返回以下行 startDate - endDate 2012-10-25 - 2012-10-25 2011-09-10 - 2013-11-15 2012-10-20 - 2012-10-25 2012-10-23 - 2012-10-28 2012-09-14 - 2012-10-28 从以下行: sta
startDate - endDate
2012-10-25 - 2012-10-25
2011-09-10 - 2013-11-15
2012-10-20 - 2012-10-25
2012-10-23 - 2012-10-28
2012-09-14 - 2012-10-28
从以下行:
startDate - endDate
2012-10-25 - 2012-10-25
2011-09-10 - 2013-11-15
2012-01-11 - 2012-10-11
2012-10-20 - 2012-10-25
2012-04-15 - 2012-04-16
2012-05-20 - 2012-05-25
2012-12-01 - 2012-12-10
2012-10-23 - 2012-10-28
2012-09-14 - 2012-10-28
2012-11-13 - 2012-12-15
这在sql中是可能的吗
我正在使用sql server 2008。对于sql server,它实际上非常简单:
SELECT startDate, endDate
FROM YourTable
WHERE '2012-10-25' between startDate and endDate
检查关键字
语法很简单:
SELECT col1, col2
FROM table1
WHERE date_col BETWEEN '2012-10-25' and 2012-10-28
对于其他重叠检查,以下内容可能会很有趣
Select * from sted where [dbo].[F_LappingDays](Startdate,EndDate,'20121025','20121025')=1
CREATE Function [dbo].[F_LappingDays](@Von1 datetime,@bis1 Datetime,@von2 Datetime,@bis2 Datetime) Returns int as
/*
20110531 Thomas Wassermann
Terminüberschneidungen finden
*/
begin
Declare @Result int
Select @Result = 0
if (@Von1>=@Von2) and (@bis1<=@Bis2)
begin
Select @Result=Cast(@Bis1 - @von1 + 1 as Int)
end
else if (@Von1<=@Von2) and (@bis1 > @Von2) and (@bis1<=@Bis2)
begin
Select @Result=Cast(@Bis1 - @von2 + 1 as Int)
end
else if (@Von1>=@Von2) and (@von1<=@bis2) and (@bis1>@Bis2)
begin
Select @Result=Cast(@Bis2 - @von1 + 1 as Int)
end
else if (@Von1<@Von2) and (@bis1>@Bis2)
begin
Select @Result=Cast(@Bis2 - @von2 + 1 as Int)
end
Return @Result
end
如果我有两个日期,发现两个日期之间的日期在此开始日期之间是否可用。开始日期为1,结束日期为10,如果我有两个日期3作为开始日期,6作为结束日期,该怎么办。那么,我可以找到3和6在1和10之间。如何在查询中添加两个日期,如“2012-10-25”、“2012-10-26”?