在SQL中的两个日期之间获取几个月

时间:2015-01-13 20:18:29

标签: sql sql-server tsql date datetime

我有一个返回以下内容的函数:

Title     Start          End
Task A    2015-01-02     2015-03-31
Task B    2015-02-12     2015-04-01
Task C    2014-11-01     2015-02-05
....

我想为每个月返回一列,如果它在开始和结束时段0之内,则返回1,否则

 Title     Start          End          Jan   Feb  Mar  Apr  May  Jun  ....
 Task A    2015-01-02     2015-03-31    1     1    1    0    0    0
 Task B    2015-02-12     2015-04-01    0     1    1    1    0    0 
 Task C    2014-11-01     2015-02-05    1     1    0    0    0    0
 ....

任何人都知道如何做到这一点?

3 个答案:

答案 0 :(得分:1)

您可以使用基本case语句执行此操作:

select title, start, end,
       (case when 1 between month(start) and month(end) then 1 else 0 end) as jan,
       (case when 2 between month(start) and month(end) then 1 else 0 end) as feb,
       . . .
       (case when 12 between month(start) and month(end) then 1 else 0 end) as dec
from table t;

注意:我在查询中保留列名,即使有些是保留字,也应该进行转义(如果这是列的真实名称)。

另请注意,在样本数据中,日期在第一个表和第二个表之间发生变化。

答案 1 :(得分:1)

如果您只想检查1个日期,这将有效。您应该能够调整此样本以满足您的需求。

SELECT c.CreateDateUTC, DATEPART(MONTH, c.CreateDateUTC) 'MONTH',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 1 THEN 1 
    END 'JAN',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 2 THEN 1 
    END 'FEB',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 3 THEN 1 
    END 'MAR',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 4 THEN 1 
    END 'APR',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 5 THEN 1 
    END 'MAY',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 6 THEN 1 
    END 'JUN',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 7 THEN 1 
    END 'JUL',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 8 THEN 1 
    END 'AUG',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 9 THEN 1 
    END 'SEP',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 10 THEN 1 
    END 'OCT',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 11 THEN 1 
    END 'NOV',
    CASE DATEPART(MONTH, c.CreateDateUTC)
    WHEN 12 THEN 1 
    END 'DEC'   
FROM dbo.Code c

结果:

enter image description here

答案 2 :(得分:0)

要进行扩展,请确保检查为null,并且可以使用ISNULL(StartDate,GetDate()),如果符合您的范围需求,它将在今天为您提供。

select *, 
    case when StartDate is not null and EndDate is not null and 1 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Jan,
    case when StartDate is not null and EndDate is not null and 2 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Feb,
    case when StartDate is not null and EndDate is not null and 3 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Mar,
    case when StartDate is not null and EndDate is not null and 4 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Apr,
    case when StartDate is not null and EndDate is not null and 5 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end May,
    case when StartDate is not null and EndDate is not null and 6 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Jun,
    case when StartDate is not null and EndDate is not null and 7 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Jul,
    case when StartDate is not null and EndDate is not null and 8 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Aug,
    case when StartDate is not null and EndDate is not null and 9 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Sep,
    case when StartDate is not null and EndDate is not null and 10 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Oct,
    case when StartDate is not null and EndDate is not null and 11 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Nov,
    case when StartDate is not null and EndDate is not null and 12 between MONTH(StartDate) and Month(EndDate) then 1 else 0 end Dec
from Foo