获取比较endate与当前日期的数据

时间:2015-09-03 06:59:54

标签: sql sql-server

我有这样的查询:

select F_Exhibition_Name, F_dtFrom as startdate, F_dtTo as enddate
from T_Exhibition

我的输出如下:

Exhibiton  startdate   enddate
A          2015-05-04  2015-05-21
B          2015-06-10  2015-06-20
C          2015-07-10  2015-09-11

与现在的日期相比,我希望在结束日期后7天内获得展览名称。

3 个答案:

答案 0 :(得分:0)

请使用以下查询

 select F_Exhibition_Name,F_dtFrom as startdate,CASE WHEN CONVERT(date,F_dtTo)=CONVERT(DATE,GETDATE()) THEN DATEADD(DAY,7,F_dtTo) else F_dtTo end   as enddate from T_Exhibition 

答案 1 :(得分:0)

以下查询将使用Getdate()检查当前日期后显示截至7天的事件:

select F_Exhibition_Name from T_Exhibition where   Getdate() < = DATEADD(day,7,enddate )

例如:

案例1:

Event end date  : 7
Current date    : 1
Show event till : 7 + 7 = 14
So it will be displayed on 1 (1<14)

案例2:

Event end date  : 8
Current date    : 18
Show event till : 8 + 7 = 15
So it will be not be displayed on as 8 > 15

希望能够解决这个问题。

答案 2 :(得分:0)

我假设您希望F_dtToF_dtFrom至少大7天:

SELECT F_Exhibition_Name,
       F_dtFrom AS startdate
       F_dtTo AS enddate
FROM T_Exhibition
WHERE DATEDIFF(day, F_dtFrom , F_dtTo) >= 7;

This MSDN link有更多有用的日期时间函数。