How to get total number of Sunday's for given date 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.
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).
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With