Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting by month in PostgreSQL

I want to select rows according to the month of a date or timestamp column like this:

SELECT id, name, birthday  FROM employee.person  WHERE Month(birthday) > 10; 

But I only get error messages in PostgreSQL.
How can this be done?

like image 841
aulia Avatar asked Jan 14 '12 15:01

aulia


People also ask

How do I sort by month name in PostgreSQL?

The TO_DATE(birthday_month, 'Month') function converts a full month name to a date in the ' 0001-MM-01 ' format. For example, you get ' 0001-12-01 ' for December. You can now use the EXTRACT(MONTH FROM date) function to extract the month from this date value.

How do I select data by month in SQL?

To select all entries from a particular month in MySQL, use the monthname() or month() function.

Can we group by month in SQL?

Group By Month and Year We also use YEAR() and MONTH() functions to ensure that data is grouped and ordered by month and year numbers. In the above query, if we use date_format function in group by clause, then MySQL will sort the groups alphabetically, instead of chronologically.

How do I select values in PostgreSQL?

If you want to select data from all the columns of the table, you can use an asterisk ( * ) shorthand instead of specifying all the column names. The select list may also contain expressions or literal values. Second, specify the name of the table from which you want to query data after the FROM keyword.

How to get the month from a date in PostgreSQL?

In PostgreSQL you can use the EXTRACT () function to get the month from a date. You can also use the DATE_PART () function to do the same thing. Example 1: The EXTRACT () Function Here’s an example of using the EXTRACT () function to extract the month from a date.

What is month-level precision in PostgreSQL?

The month-level precision causes the day to be displayed as '01' and the time to be filled with zeros. (The truncated date parts (e.g. days, months) of the timestamp are replaced with ones.) Grouping records by month is very common in PostgreSQL. In our example, the number of products is totaled for each month.

How do I retrieve a date with a specific precision from PostgreSQL?

Use the DATE_TRUNC () function if you want to retrieve a date or time with a specific precision from a PostgreSQL database. (In our example, we used month precision.) This function takes two arguments. First, we have the date part specifier (in our example, 'month'). Note that the specifier is a string and needs to be enclosed in quotes.

How do I extract a year from a date in PostgreSQL?

extract (year from date) as yyyy : Postgresql's "extract" function is used to extract the YYYY year from the "date" attribute. sum ("Sales") as "Sales" : The SUM () function adds up all the "Sales" values, and supplies a case-sensitive alias, with the case sensitivity maintained by using double-quotes.


2 Answers

You can use EXTRACT function, like this:

SELECT id, name, birthday FROM employee.person  WHERE EXTRACT(MONTH FROM birthday) > 10; 

Your problem comes from the fact that there is no such thing as Month function in PostgreSQL. Check online documentation here to see what you can get instead. Extract should be enough.

like image 125
k.m Avatar answered Sep 25 '22 01:09

k.m


If you want you can also extract the month name using the following function.

SELECT TO_CHAR(DATE(REPORT_DATE), 'Month') FROM TABLE_NAME 
like image 29
Anish Jain Avatar answered Sep 24 '22 01:09

Anish Jain