You can truncate a date in Postgres with the function date_trunc
. In a simple way it allows you to ignore a part of the date/time and focus only on the part you want.
An example of use:
SELECT date_trunc('month', current_date) as este_mes;
este_mes
------------------------
2014-04-01 00:00:00-03
Note that he truncated today’s date and the result was the first day of this month because the keyword was used month
, but you can truncate on time, minute or even year. It’s a good alternative so you don’t need to use the fução extract
more than once.
So you can use this feature for your query more or less like this:
SELECT sum(...) as soma
FROM ...
WHERE date_trunc('month', data) = date_trunc('month', current_date);
For more date and time manipulation functions see http://www.postgresql.org/docs/current/static/functions-datetime.html.