How can I find the last day of last month in SQL?

Moderator: NorbertKrupa

Post Reply
User avatar
Jbaskin
Intermediate
Intermediate
Posts: 61
Joined: Sat Jan 28, 2012 1:21 pm

How can I find the last day of last month in SQL?

Post by Jbaskin » Wed Feb 08, 2012 10:54 pm

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.

User avatar
JimKnicely
Site Admin
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?

Post by JimKnicely » Thu Feb 09, 2012 1:21 pm

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:

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

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

JeffSatler
Newbie
Newbie
Posts: 21
Joined: Mon Feb 13, 2012 9:44 pm

Re: How can I find the last day of last month in SQL?

Post by JeffSatler » Sat Mar 24, 2012 12:01 pm

Wow, I needed this, thanks!

Post Reply

Return to “New to Vertica Database Development”