HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Search for current month data in postgresql 9.2

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlsearchforcurrentmonthdata

Problem

I'm trying to get the ticket data only from the current month on a PostgreSql 9.2 database.

The field called data_cadastro is DATETIME:

id_ticket |    data_cadastro
-----------+---------------------
      2521 | 2017-10-31 08:11:48


how should I do ?

Solution

Just use extract

test=# SELECT extract(month FROM '2017-10-31 08:11:48'::timestamp);
 date_part 
-----------
        10
(1 row)


If you want all tickets for that month,

SELECT *
FROM ticketdata
WHERE extract(month FROM data_cadastro) = 10;


If you want only that year and month, just do this.

SELECT *
FROM ticketdata
WHERE date_trunc('month', data_cadastro)::date = '2017-10-01'::date;

Code Snippets

test=# SELECT extract(month FROM '2017-10-31 08:11:48'::timestamp);
 date_part 
-----------
        10
(1 row)
SELECT *
FROM ticketdata
WHERE extract(month FROM data_cadastro) = 10;
SELECT *
FROM ticketdata
WHERE date_trunc('month', data_cadastro)::date = '2017-10-01'::date;

Context

StackExchange Database Administrators Q#190774, answer score: 7

Revisions (0)

No revisions yet.