April 14, 2016 at 10:36 am
Hi,
I'd like to flag my data to let me know if a date is the last day of the month.
So, a simple flag which would indicate True or False.
Select Date, Is The Date Last Day of the month?
From Where ever
3/31/2016 | True
4/1/2016 | False
Thanks
April 14, 2016 at 10:53 am
CASE WHEN MONTH(YourDate) <> MONTH(DATEADD(day, 1, YourDate)) THEN 'True' ELSE 'False' END
April 14, 2016 at 11:23 am
Hugo Kornelis (4/14/2016)
CASE WHEN MONTH(YourDate) <> MONTH(DATEADD(day, 1, YourDate)) THEN 'True' ELSE 'False' END
Fixed That For You
April 14, 2016 at 11:33 am
A different option.
SELECT YourDate,
CASE WHEN MONTH(YourDate) <> MONTH(DATEADD(day, 1, YourDate)) THEN 'True' ELSE 'False' END,
CASE WHEN YourDate = DATEADD( MM, DATEDIFF( MM, 0, YourDate), 30) THEN 'True' ELSE 'False' END
FROM( VALUES( '3/31/2016'), ('4/1/2016'))x(YourDate);
April 20, 2016 at 9:00 am
Luis Cazares (4/14/2016)
A different option.
SELECT YourDate,
CASE WHEN MONTH(YourDate) <> MONTH(DATEADD(day, 1, YourDate)) THEN 'True' ELSE 'False' END,
CASE WHEN YourDate = DATEADD( MM, DATEDIFF( MM, 0, YourDate), 30) THEN 'True' ELSE 'False' END
FROM( VALUES( '3/31/2016'), ('4/1/2016'))x(YourDate);
This 2nd suggestion works but it assumes that YourDate does not have a time component (or time must be 0:00) or it wil not work. Hugo's suggestion was simple and works for any datetime value on any month's last day. So I suggest you better use that 1st suggestion.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply