Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data for previous month in postgresql

Tags:

postgresql

SELECT * FROM Conference WHERE date_start ---don't know how to proceed--- 

How to check if date_start has been in the previous month ? Thanks you.

like image 210
Ilian Vasilev Kulishev Avatar asked Jun 02 '16 09:06

Ilian Vasilev Kulishev


People also ask

How do I get last 3 months Postgres?

How to get last 3 months records from the table. SELECT * from table where month > CURRENT_DATE-120 and month < CURRENT_DATE order by month; I have used the above query is it correct?

How do you get the last date of the month in Postgres?

You can select the last day of month. SELECT (date_trunc('MONTH', ('201608'||'01')::date) + INTERVAL '1 MONTH - 1 day')::DATE; 201608 is replaceable with a variable.

What is now () in PostgreSQL?

What is PostgreSQL Now Function? The Now() function is used to return the current date and time of the time zone (default or user-defined). Its return type is the timestamp with the time zone.


1 Answers

Subtract one month from the current month, then "truncate" that to the beginning of that date. As you don't want to include rows from "this" month, you also need to add a condition for that

SELECT * FROM Conference  WHERE date_start >= date_trunc('month', current_date - interval '1' month)   and date_start < date_trunc('month', current_date) 

date_trunc('month', current_date - interval '1' month) will return the 1st day of the previous month and date_trunc('month', current_date) will return the first day of "this" month.

like image 108
a_horse_with_no_name Avatar answered Sep 25 '22 16:09

a_horse_with_no_name