Hi y'all,
I found a last_day() function, but not a first_day() function. Does one exist? How can I find the first day of the month for a given date?
First day of the month function?
Moderator: NorbertKrupa
Re: First day of the month function?
Never mind, I think I figured this out myself
Code: Select all
dbadmin=> select last_day('2013-08-10') + 1 as first_day;
first_day
------------
2013-09-01
(1 row)
Re: First day of the month function?
HI!
FYI: your solution gives NEXT month
FYI: your solution gives NEXT month
Code: Select all
daniel=> select date(date_trunc('MONTH', now()));
date
------------
2013-10-01
(1 row)
daniel=> select to_date('2013-08-10', 'YYYY-MM-01');
to_date
------------
2013-08-01
(1 row)
daniel=> select to_date(now()::varchar, 'YYYY-MM-01');
to_date
------------
2013-10-01
(1 row)
Re: First day of the month function?
Yikes! Thanks for noticing my error sKwa I'll use the your solution.
- JimKnicely
- Site Admin
- Posts: 1825
- Joined: Sat Jan 21, 2012 4:58 am
- Contact:
Re: First day of the month function?
beth,
Don't forget you can create your own function too!
(using one of sKwa's solution):
Don't forget you can create your own function too!
(using one of sKwa's solution):
Code: Select all
dbadmin=> CREATE FUNCTION first_day(x DATE) RETURN DATE
dbadmin-> AS
dbadmin-> BEGIN
dbadmin-> RETURN DATE(DATE_TRUNC('MONTH', x));
dbadmin-> END;
CREATE FUNCTION
dbadmin=> SELECT now()::date now, first_day(now()::date) first_day;
now | first_day
------------+------------
2013-10-09 | 2013-10-01
(1 row)
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.
Re: First day of the month function?
Hi!
[DELETED]
[DELETED]
Last edited by id10t on Sat May 09, 2015 3:34 pm, edited 1 time in total.
Re: First day of the month function?
Cool, thanks guys! Are you brothers or something