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

Get months between two dates in TSQL

我有一个函数 return 如下:

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
....

我想 return 每个月的一列,如果在开始和结束期间内则为 1,否则为 0

 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
 ....

有人知道如何做到这一点吗?

您可以使用基本的 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;

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

另请注意,在您的示例数据中,日期在第一个 table 和第二个之间发生变化。

如果您只想检查 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

结果:

要扩展,请确保检查是否为 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