Solved

Calculate Working Days between dates

Posted on 2010-11-12
2
1,827 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 500 total points
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
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…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now