混淆案例陈述价值

Confused Case Statement Value

select
    -----
    date_tbl.[Date Name]
    -----
    ,case
        when month(date_tbl.[Date Name]) = 1 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 1 Start')
        when month(date_tbl.[Date Name]) = 1 and day(date_tbl.[Date Name]) = 31 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 1 End')
        -----
        when month(date_tbl.[Date Name]) = 2 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 2 Start')
        when month(date_tbl.[Date Name]) = 2 and (day(eomonth(date_tbl.[Date Name])) = 28 OR day(eomonth(date_tbl.[Date Name])) = 29) then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 2 End')
        -----
        when month(date_tbl.[Date Name]) = 3 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 3 Start')
        when month(date_tbl.[Date Name]) = 3 and day(date_tbl.[Date Name]) = 31 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 3 End')
        -----
        when month(date_tbl.[Date Name]) = 4 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 4 Start')
        when month(date_tbl.[Date Name]) = 4 and day(date_tbl.[Date Name]) = 30 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 4 End')
        -----
        when month(date_tbl.[Date Name]) = 5 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 5 Start')
        when month(date_tbl.[Date Name]) = 5 and day(date_tbl.[Date Name]) = 31 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 5 End')
        -----
        when month(date_tbl.[Date Name]) = 6 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 6 Start')
        when month(date_tbl.[Date Name]) = 6 and day(date_tbl.[Date Name]) = 30 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 6 End')
        -----
        when month(date_tbl.[Date Name]) = 7 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 7 Start')
        when month(date_tbl.[Date Name]) = 7 and day(date_tbl.[Date Name]) = 31 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 7 End')
        -----
        when month(date_tbl.[Date Name]) = 8 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 8 Start')
        when month(date_tbl.[Date Name]) = 8 and day(date_tbl.[Date Name]) = 31 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 8 End')
        -----
        when month(date_tbl.[Date Name]) = 9 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 9 Start')
        when month(date_tbl.[Date Name]) = 9 and day(date_tbl.[Date Name]) = 30 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 9 End')
        -----
        when month(date_tbl.[Date Name]) = 10 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 10 Start')
        when month(date_tbl.[Date Name]) = 10 and day(date_tbl.[Date Name]) = 31 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 10 End')
        -----
        when month(date_tbl.[Date Name]) = 11 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 11 Start')
        when month(date_tbl.[Date Name]) = 11 and day(date_tbl.[Date Name]) = 30 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 11 End')
        -----
        when month(date_tbl.[Date Name]) = 12 and day(date_tbl.[Date Name]) = 1 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 12 Start')
        when month(date_tbl.[Date Name]) = 12 and day(date_tbl.[Date Name]) = 31 then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 12 End')
        -----
        else NULL
        end as [month_label]
    -----
from
    [dbo].[DATES] as date_tbl

我的结果是这样的:

Date Name     month_label
1900-01-01    1900 - Month 1 Start
1900-01-02    NULL
...           ...
1900-01-31    1900 - Month 1 End
1900-02-01    1900 - Month 2 Start
1900-02-02    1900 - Month 2 End
1900-02-03    1900 - Month 2 End
1900-02-04    1900 - Month 2 End
1900-02-28    1900 - Month 2 End
1900-03-01    1900 - Month 3 Start
1900-03-02    NULL
.....         .....
1900-03-31    1900 - Month 3 End

为什么我的 2 月标签的每一行都有“结束”值,而不是该月的最后一天?我如何修复它以便它只标记每个月的第一天和最后一天?

因为您在 2 月的案例陈述中有 eomonth 函数。 EOMONTH 函数给出任何日期的最后一个月。所以所有日期都满足最后一个日期 case 语句。

您需要更改以下行

when month(date_tbl.[Date Name]) = 2 and (day(eomonth(date_tbl.[Date Name])) = 28 OR day(eomonth(date_tbl.[Date Name])) = 29) then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 2 End')

    when month(date_tbl.[Date Name]) = 2 and (day(date_tbl.[Date Name]) = 28 OR day(date_tbl.[Date Name]) = 29) then concat(cast(year(date_tbl.[Date Name]) as varchar(255)),' - ','Month 2 End')

您不需要所有这些 CASE 每个月的表达式。

只需检查月初的 DAY (DateCol) = 1 和月末的 DateCol = EOMONTH(DateCol)

CONCAT(DATENAME(year, [DateCol ]),
       ' - Month ',
       month([DateCol ]),
       case when day([DateCol ]) = 1             then ' Start'
            when [DateCol] = EOMONTH([DateCol ]) then ' End'
            end) as [month_label]