Solved

Calculate Working Days between dates

Posted on 2010-11-12
2
1,923 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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