Solved

Calculate Working Days between dates

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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