It is possible to get data from the entire month in postgreSQL

2

How do I get all the data stored in the database from a specific month: I think you should pass the year / month as a parameter, for example, variable="2016/10"; remembering that the type of the data in the database and timestamp

  SELECT * FROM dados WHERE data_registro = variavel
    
asked by anonymous 20.10.2016 / 20:00

3 answers

3

Option 1: Using TO_CHAR :

select * from dados where TO_CHAR(data_registro, 'MM/YYYY') = '01/2016'

Option 2: Use with ranges:

select * from dados where data_registro >= '2016-01-01'::DATE 
and data_registro <= '2016-01-31'::DATE

Option 3: Using overlaps

select * from dados where (data_registro, data_registro) 
OVERLAPS ('2016-01-01'::DATE, '2016-01-31'::DATE);

Option 4: With Extract :

select * from dados 
where 
Extract(month from data_registro) >= Extract(month from '2016-01-01'::DATE)
and Extract(month from data_registro) <= Extract(month from '2016-01-31'::DATE)

Option 5: Use of intervals based on current date:

SELECT *
FROM   dados
WHERE  Extract(month from data_registro) >= Extract(month from Now())
AND    Extract(month from data_registro) <= Extract(month from Now() + Interval '20 day')
    
20.10.2016 / 20:10
1

Just giving continuity to the options already mentioned

Option with date_trunc

SELECT * FROM dados WHERE 
   date_trunc('month', data_registro) = date_trunc('month', '2015-03-01');

In this case the parameter can be any valid date in the month, it will truncate both for the same month.

Reference

    
20.10.2016 / 20:38
0

You can use BETWEEN :

SELECT dados
FROM   tabela
WHERE  tabela.data BETWEEN '2016-01-01' AND '2016-01-31'

If the field in question is a TIMESTAMP type, depending on your BD, you should remember to do CAST (eg Postgres tabela.data::DATE )

    
20.10.2016 / 20:43