?
Solved

Calculate Working Days between dates

Posted on 2010-11-12
2
Medium Priority
?
2,068 Views
Last Modified: 2012-05-10
Hello Experts,

I am working on a Postgres DB that gets populated with data from Excel files.
In this DB I have a table with some columns with a type of timestamp without time zone
like: Creation_Date_UTC and Closure_date_UTC

What I want is maybe a function able to calculate "Working Days" (Bussines days <Monday to Friday>) from the interval in those two columns (or others I specify in the query).

ie: get_working_days(Creation_Date_UTC, Closure_date_UTC)

My date format in postgres is DD/MM/YYYY.

Thanks so much!
0
Comment
Question by:daniel_spiri
2 Comments
 
LVL 7

Accepted Solution

by:
Hatrix76 earned 2000 total points
ID: 34125433
CREATE OR REPLACE FUNCTION get_working_days (in date, in date) RETURNS int8 AS
$BODY$
SELECT count(*) FROM
(SELECT extract('dow' FROM $1+x) AS dow
FROM generate_series(0,$2-$1) x) AS foo
WHERE dow BETWEEN 1 AND 5;
$BODY$
LANGUAGE 'sql'
GO


best,

ray
0
 

Author Closing Comment

by:daniel_spiri
ID: 34127303
Thanks!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question