Get first date of month in postgres
Asked Answered
I

6

84

I'm trying to get a 'date' type that corresponds to the first day of the current month. Basically one of my tables stores a date, but I want it to always be the first of the month, so I'm trying to create a trigger that will get now() and then replace the day with a 1.

Inamorata answered 5/8, 2013 at 23:8 Comment(0)
F
166

You can use the expression date_trunc('month', current_date). Demonstrated with a SELECT statement . . .

select date_trunc('month', current_date)
2013-08-01 00:00:00-04

To remove time, cast to date.

select cast(date_trunc('month', current_date) as date)
2013-08-01

If you're certain that column should always store only the first of a month, you should also use a CHECK constraint.

create table foo (
  first_of_month date not null
  check (extract (day from first_of_month) = 1)
);

insert into foo (first_of_month) values ('2015-01-01'); --Succeeds
insert into foo (first_of_month) values ('2015-01-02'); --Fails
ERROR:  new row for relation "foo" violates check constraint "foo_first_of_month_check"
DETAIL:  Failing row contains (2015-01-02).
Finkle answered 5/8, 2013 at 23:14 Comment(1)
I used to do it like this select cast((now() - (extract(day from now())) * interval '1 day') + interval '1 day' as date) as first_of_month but this helped. Thanks!Salivate
T
35

date_trunc() will do it.

SELECT date_trunc('MONTH',now())::DATE;

http://www.postgresql.org/docs/current/static/functions-datetime.html

Transitory answered 5/8, 2013 at 23:15 Comment(0)
I
10

You can also use TO_CHAR to get the first day of the month:

SELECT TO_CHAR(some_date, 'yyyy-mm-01')::date
Ineducable answered 18/10, 2018 at 9:21 Comment(0)
H
6

Found this to get the first day of that month and the last date of that month

select date_trunc('month', current_date-interval '1 year'), date_trunc('month', current_date-interval '1 year')+'1month'::interval-'1day'::interval;
Horacehoracio answered 6/9, 2019 at 13:57 Comment(2)
Why -interval '1 year'?Oys
That is for getting previous year, first and last day of a month based on the current time. It can be removed to get the current year like select date_trunc('month', current_date-interval '1 year'), date_trunc('month', current_date)+'1month'::interval-'1day'::interval;Horacehoracio
E
2

SELECT TO_DATE('2017-12-12', 'YYYY-MM-01');

2017-12-01

Equidistance answered 4/10, 2017 at 6:45 Comment(4)
What is the problem with this answer?Equidistance
probably just that you used a static data rather than the CURRENT_DATE()?Singletary
that is the problem: LINE 1: SELECT TO_DATE("t1"."date", 'YYYY-MM-01') AS ddate, Sum("t1"... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.Loki
You should use to_char instead e.g. SELECT TO_CHAR('2017-12-12', 'YYYY-MM-01')::dateIneducable
K
0

date_trunc() always returns the first day of provided type.

for example

SELECT date_trunc('MONTH',now())::DATE will return the first day of month;

SELECT date_trunc('YEAR',now())::DATE will return the first day of year;

SELECT date_trunc('CENTURY',now())::DATE will return the first day of century;

::DATE changes the format of returned data to date format YYYY-MM-DD

Kaolinite answered 22/11, 2023 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.