-1

我们确实有一个表事件并使用以下列存储事件

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

请注意:我们不存储年份值,因为事件每年都会发生,因此 endmonth 可能小于 beginmonth(记录号 3)。这意味着活动从一年开始,到下一年结束

现在我想要一个 ms sql 查询,它应该根据当前日期给出所有事件。如果我们不使用年份作为当前日期会很好。

4

1 回答 1

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 小提琴

更新的小提琴

我得到了我的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 小提琴

我希望这一次你会满意。

如果您不想使用Today'sDate 并且您有Predefined日期,请替换GETDATE()为您的predefinedDatetime 值。

是的,它Datetime

于 2013-06-10T13:54:08.730 回答