pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Function overlaps_interval
2+ messages / 2 participants
[nested] [flat]

* Function overlaps_interval
@ 2001-04-12 18:23 Josh Berkus <josh@agliodbs.com>
  2001-04-12 18:55 ` Re: Function overlaps_interval Roberto Mello <rmello@cc.usu.edu>
  0 siblings, 1 reply; 2+ messages in thread

From: Josh Berkus @ 2001-04-12 18:23 UTC (permalink / raw)
  To: pgsql-sql

Folks,

	I wrote this PL/pgSQL function for my current project, and thought it
would be generally useful.  An expansion of the builtin
overlaps(dt1,dt2,dt3,dt4) function, this function returns the interval
of time for which the two datetime ranges overlap.

	Roberto, please include this in your online PL/pgSQL function library.

CREATE FUNCTION overlap_interval(DATETIME, DATETIME, DATETIME, DATETIME)
RETURNS INTERVAL AS '
DECLARE
	begin1 ALIAS for $1;
	end1 ALIAS for $2;
	begin2 ALIAS for $3;
	end2 ALIAS for $4;
	overlap_amount INTERVAL;
BEGIN
	--test for overlap using the ovelap function.
	--if not found, return 0 interval.

	IF NOT overlaps(begin1, end1, begin2, end2) THEN
		RETURN ''00:00:00''::INTERVAL;
	END IF;

	--otherwise, test for the various forms of overlap

	IF begin1 < begin2 THEN
		IF end1 < end2 THEN
			overlap_amount := end1 - begin2;
		ELSE
			overlap_amount := end2 - begin2;
		END IF;
	ELSE
		IF end1 < end2 THEN
			overlap_amount := end1 - begin1;
		ELSE
			overlap_amount := end2 - begin1;
		END IF;
	END IF;

	RETURN overlap_amount;
END;'
LANGUAGE 'plpgsql';
			
-Josh Berkus


______AGLIO DATABASE SOLUTIONS___________________________
                                       Josh Berkus
  Complete information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Function overlaps_interval
  2001-04-12 18:23 Function overlaps_interval Josh Berkus <josh@agliodbs.com>
@ 2001-04-12 18:55 ` Roberto Mello <rmello@cc.usu.edu>
  0 siblings, 0 replies; 2+ messages in thread

From: Roberto Mello @ 2001-04-12 18:55 UTC (permalink / raw)
  To: Josh Berkus <josh@agliodbs.com>; +Cc: pgsql-sql

On Thu, Apr 12, 2001 at 11:23:28AM -0700, Josh Berkus wrote:
> Folks,
> 
> 	I wrote this PL/pgSQL function for my current project, and thought it
> would be generally useful.  An expansion of the builtin
> overlaps(dt1,dt2,dt3,dt4) function, this function returns the interval
> of time for which the two datetime ranges overlap.
> 
> 	Roberto, please include this in your online PL/pgSQL function library.

	Done! Thanks Josh! Date/Time manipulation functions are now our most
popular category.
	
	How about you? Do you have a (SQL|PL/pgSQL|PL/Perl|PL/Tcl|PL/Python)
function that you are proud of? A function that makes your life easier?
Then waste no more time! Point your browser to

	http://www.brasileiro.net/postgres 

	submit your recipe and join the CookBook Hall of Fame! You get your
own awesome-cool "PostgreSQL Powered" button!

	We've been getting many hits from techdocs.postgresql.org, which shows
that many are going to techdocs looking for solutions to common problems.
Very useful.

	-Roberto
-- 
+----| http://fslc.usu.edu USU Free Software & GNU/Linux Club |------+
  Roberto Mello - Computer Science, USU - http://www.brasileiro.net 
       http://www.sdl.usu.edu - Space Dynamics Lab, Developer    
Bad command or file name. Go sit in corner.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2001-04-12 18:55 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-04-12 18:23 Function overlaps_interval Josh Berkus <josh@agliodbs.com>
2001-04-12 18:55 ` Roberto Mello <rmello@cc.usu.edu>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox