user1813375
user1813375

Reputation: 101

How to obtain records of a specific month in SQL by date

I'm using Postgres 9.1 and try to get records from a table knowing the given month and should know if the date type date field contains the month.

This is my query:

SELECT consejo.numero,consejo.fecha FROM consejo WHERE MONTH(consejo.fecha) = 2

but I get the following error:

ERROR: There is no month function (date)
LINE 1: ... T consejo.numero, consejo.fecha council FROM WHERE MONTH (cons ...

HINT: No function matches the name and types of arguments. You may need to add explicit type conversion.

might be wrong?

Upvotes: 1

Views: 2067

Answers (2)

Rodrigo Hjort
Rodrigo Hjort

Reputation: 301

Another option is to use PostgreSQL's date_part() function:

 WHERE date_part('month', consejo.fecha) = 2

Look at the reference guide:

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

Upvotes: 0

Giorgos Betsos
Giorgos Betsos

Reputation: 72175

In Postgresql there is no MONTH function available. You can use EXTRACT instead:

WHERE EXTRACT(MONTH FROM consejo.fecha) = 2

Upvotes: 3

Related Questions