How can I find the last day of last month in SQL?
Moderator: NorbertKrupa
How can I find the last day of last month in SQL?
How can I find the last day of last month using SQL in Vertica? For instance, last month was January and the last day was the 31st. I'd like to get back 31. And for next month, the answer would be 29. Thanks.
- JimKnicely
- Site Admin
- Posts: 1825
- Joined: Sat Jan 21, 2012 4:58 am
- Contact:
Re: How can I find the last day of last month in SQL?
Although there isn't a built in function for that, it's easy enough to get the result you need via a combination of built in functions.
For instance in vSQL:
For instance in vSQL:
Code: Select all
vertica01=> SELECT CURRENT_DATE() "TODAY", DAY(LAST_DAY(ADD_MONTHS(CURRENT_DATE, -1))) "LAST DAY OF LAST MONTH";
TODAY | LAST DAY OF LAST MONTH
------------+------------------------
2012-02-09 | 31
Jim Knicely
Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.
Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.
-
- Newbie
- Posts: 21
- Joined: Mon Feb 13, 2012 9:44 pm
Re: How can I find the last day of last month in SQL?
Wow, I needed this, thanks!