Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the count of current month Sunday's in psql?

Tags:

postgresql

How to get total number of Sunday's for given date in postgresql

like image 319
ungalnanban Avatar asked Feb 17 '11 14:02

ungalnanban


People also ask

What is count (*) in PostgreSQL?

1) COUNT(*) You can use the PostgreSQL COUNT(*) function along with a SELECT statement to return the total number of rows in a table including the NULL values as well as the duplicates.

How do I get the current week number in PostgreSQL?

Use the DATE_PART() function to retrieve the week number from a date in a PostgreSQL database. This function takes two arguments. The first argument is the date part to retrieve; we use 'week', which returns the week number (e.g. “1” for the first week in January, the first week of the year).

What is Getdate in PostgreSQL?

GETDATE function returns the current date and time. There is no GETDATE function in PostgreSQL, but there is NOW() function for the same purpose. If there are multiple occurrences of the GETDATE function then you can automate them using extension.


1 Answers

You need EXTRACT:

SELECT 
    EXTRACT(DOW FROM DATE '2011-02-16') = 0; -- 0 is Sunday

This can result in true or false, it's a sunday or it's not. I have no idea what you mean by "total number" because that will always be 0 (the date is not a sunday) or 1 (the given data is a sunday).

Edit: Something like this?

SELECT 
    COUNT(*)
FROM
    generate_series(timestamp '2011-01-01', '2011-03-01', '1 day') AS g(mydate)
WHERE
    EXTRACT(DOW FROM mydate) = 0;
like image 139
Frank Heikens Avatar answered Nov 15 '22 08:11

Frank Heikens