首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查给定日期是否符合某一日期范围

检查给定日期是否符合某一日期范围
EN

Stack Overflow用户
提问于 2012-10-31 23:09:48
回答 4查看 92.8K关注 0票数 21

如果我在一个表中有两个日期列,startDateendDate。如何返回给定日期在这两个日期之间的行?例如:

如果给定日期为2012-10-25

它应该返回以下行

代码语言:javascript
复制
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

从以下行中:

代码语言:javascript
复制
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。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-10-31 23:11:57

对于SQL Server,它实际上就像下面这样简单:

代码语言:javascript
复制
SELECT startDate, endDate
FROM YourTable
WHERE '2012-10-25' between startDate and endDate
票数 59
EN

Stack Overflow用户

发布于 2012-10-31 23:15:45

检查BETWEEN关键字。

语法很简单:

代码语言:javascript
复制
SELECT col1, col2
FROM table1
WHERE '2012-10-25' BETWEEN col1 and col2

其中col1和col2分别是开始日期和结束日期。

票数 4
EN

Stack Overflow用户

发布于 2012-10-31 23:50:55

对于其他的lappingcheck,下面可能是有趣的

代码语言:javascript
复制
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 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13161044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档