SQL Server查询事件过滤器

时间:2013-06-10 13:14:58

标签: sql sql-server tsql datetime

我们有一个表事件和使用后续列​​存储事件

EventID INT
EventName VARCHAR(50)
BeginMonth TINYINT
BeginDay TINYINT
EndMonth TINYINT
EndDay TINYINT

以下是 records

的一些示例
EventID EventName  BeginMonth BeginDay EndMonth EndDay
  1      Event-1     3           1        6      17
  2      Event-2     5           10       8      15
  3      Event-3     11           5       2      13

请注意:由于事件每年都会发生,因此我们不存储年份值,因此可能会比startmonth(记录号3)更少。这意味着活动开始一年并结束下一个

现在我想要一个ms sql查询,该查询应根据当前日期提供所有事件。如果我们不使用年份作为当前日期将是好的。

1 个答案:

答案 0 :(得分:3)

如果我的问题得到了解决,则以下 Query 将适合您。

Select *

FROM Events
WHERE --CONVERT(DATE,GETDATE()) BETWEEN
CONVERT(date,dateadd(day,datediff(day,1,GETDATE()),0)) BETWEEN
CAST((CAST(case when CAST([BeginMonth] as Varchar)> (select datepart(month,getdate())) 
THEN (select datepart(YEAR,getdate())-1)
when CAST([BeginMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([BeginDay] as Varchar)> (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate())-1)
when CAST([BeginMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([BeginDay] as Varchar)= (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate()))
when CAST([BeginMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([BeginDay] as Varchar)< (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate())) 
ELSE (select datepart(YEAR,getdate())) END AS VARCHAR)
+'-'+
CAST([BeginMonth] as Varchar)
+'-'+
CAST([BeginDay] as Varchar)) as DATE) 
AND
CAST((CAST(case when CAST([BeginMonth] as Varchar)> (select datepart(month,getdate())) 
THEN (select datepart(YEAR,getdate())-1)
when CAST([BeginMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([BeginDay] as Varchar)> (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate())-1)
when CAST([BeginMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([BeginDay] as Varchar)= (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate()))
when CAST([BeginMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([BeginDay] as Varchar)< (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate())) 
ELSE (select datepart(YEAR,getdate()))+1 END AS VARCHAR)
+'-'+
CAST([BeginMonth] as Varchar)
+'-'+
CAST([BeginDay] as Varchar)) as DATE) 

<强> SQL FIDDLE

UPDATED FIDDLE

我的 Mistake.

Select *

FROM Event
WHERE CONVERT(DATE,GETDATE()) BETWEEN

CAST((CAST(case when CAST([BeginMonth] as Varchar)> (select datepart(month,getdate())) 
THEN (select datepart(YEAR,getdate())-1)
when CAST([BeginMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([BeginDay] as Varchar)> (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate())-1)
when CAST([BeginMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([BeginDay] as Varchar)= (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate()))
when CAST([BeginMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([BeginDay] as Varchar)< (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate())) 
ELSE (select datepart(YEAR,getdate())) END AS VARCHAR)
+'-'+
CAST([BeginMonth] as Varchar)
+'-'+
CAST([BeginDay] as Varchar)) as DATE) 
AND
CAST((CAST(case when CAST([EndMonth] as Varchar)> (select datepart(month,getdate())) 
THEN (select datepart(YEAR,getdate()))
when CAST([EndMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([EndDay] as Varchar)> (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate()))
when CAST([EndMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([EndDay] as Varchar)= (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate())+1)
when CAST([EndMonth] as Varchar)= (select datepart(month,getdate()))
AND CAST([EndDay] as Varchar)< (select datepart(month,getdate()))
THEN (select datepart(YEAR,getdate())+1) 
ELSE (select datepart(YEAR,getdate())) END AS VARCHAR)
+'-'+
CAST([EndMonth] as Varchar)
+'-'+
CAST([EndDay] as Varchar)) as DATE) 

<强> SQL FIDDLE

我希望这次你会满意。

如果您不想使用 Today's 日期并且 Predefined 日期,请替换 {{ 1}} GETDATE() 日期时间值。

是的 predefined