Trying to see if today is the end of the month and if it is run the code, I am thinking to use EOMONTH and getdate but I'm not sure how.
Asked
Active
Viewed 329 times
-4
-
https://stackoverflow.com/questions/32150232/sql-how-to-tell-if-a-date-is-the-last-day-of-the-month Try googling it frist next time <3 – Doug Coats Jul 13 '18 at 19:40
-
2What DBMS do you use because every DBMS has it's own methods to calculate with dates – Raymond Nijland Jul 13 '18 at 19:42
-
There is no `IF` in SQL – Jul 13 '18 at 19:48
2 Answers
1
IF(GETDATE() = EOMONTH(GETDATE()))
BEGIN
PRINT 'TODAY IS THE END OF MONTH'
END
ELSE
BEGIN
PRINT 'TODAY IS NOT THE END OF MONTH'
END

Ian-Fogelman
- 1,595
- 1
- 9
- 15
1
In MS SQL Sever GETDATE()
returns the current date and time, and CAST(GETDATE() AS DATE)
returns today's date in yyyy-mm-dd format without including the time. EOMONTH()
returns type date so EOMONTH(GETDATE())
returns the last day of the current month in yyyy-mm-dd format. Therefore if you're using a statement block:
IF CAST(GETDATE() AS DATE) = EOMONTH(GETDATE())
BEGIN
PRINT 'Today is the end of the month'
END
ELSE
BEGIN
PRINT 'Today is not the end of the month'
END
And if you're just looking for a basic IF..ELSE without the statement block:
IF CAST(GETDATE() AS DATE) = EOMONTH(GETDATE())
PRINT 'Today is the end of the month'
ELSE
PRINT 'Today is not the end of the month'

sripley
- 125
- 5