Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

My results look like this:

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

Why does my February labels have the "end" value for every row instead of the last day of the month? How do I fix it so that it only labels the 1st and last days of each month?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

because you have eomonth function in your february case statement.
The EOMONTH function gives last of the month for any date. So all dates satisfy the last date case statement.

you need to change the below line

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')

to

    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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading