Oracle – DATEDIFF Using Arithmetic

Background

Earlier this week I needed to determine the difference between two dates.

In Transact SQL, I will employ datediff.

In Transact SQL, datediff allows one to compare two dates using years, months, days, hours, minutes in terms of measurement.

Oracle

Sample

SQL


CREATE GLOBAL TEMPORARY TABLE friendBook
(
	  friendName VARCHAR2(100) NOT NULL
	, dateMet    DATE NOT NULL 
)
ON COMMIT PRESERVE ROWS;

/*
	Truncate Table
*/
TRUNCATE TABLE friendBook;

/*
	Add Data
*/
INSERT INTO friendBook
(
	  friendName
	, dateMet
)
SELECT

	  'Bob Wood'
	, TO_DATE('2023-01-01', 'YYYY-MM-DD')
	
FROM DUAL

UNION ALL

SELECT

	  'Steve Winwood'
	, TO_DATE('2023-02-01', 'YYYY-MM-DD')
	
FROM DUAL

UNION ALL

select

	  'Sam Niel'
	, SYSDATE

FROM DUAL 

UNION ALL

SELECT

	  'Terry Brahshaw'
	, TO_DATE('2023-02-07', 'YYYY-MM-DD')
	
FROM DUAL

;

/*
 * Get Data
*/
SELECT 
		  tblFB.friendName 
		  	AS "friend"
		  
		, tblFB.dateMet
		
		, TO_CHAR
			(
				SYSDATE
			) AS "nowUsingToChar"
		
		/*
		 * Convert SYSDATE to character using FORMAT
		 * FORMAT us YYYY-MM-DD HH:mi am/pm
		 * 
		*/	
		, TO_CHAR
			(
				   SYSDATE
				, 'YYYY-MM-DD  HH:mi pm'
			) AS "nowUsingToCharFormatted"		
		
		/*
		 * Days as return as fractional
		*/	
		, (
			SYSDATE
				- dateMet 
		  )
			AS "numberofDaysFractional"
			
		/*
		 * Days as return as fractional
		*/	
		,ROUND
			(
				  (
					SYSDATE
						- dateMet 
				  )
				  , 3
			)		  
			AS "numberofDaysFractional ( 3 dec. places )"
					
		
		/*
		 * Days as whole using Truncate function
		*/				
		, TRUNC
			(
				SYSDATE
					- dateMet
			) AS "numberofDaysWhole"		
		
		/*
		 * a full day in days is 1.0
		 * a full day in hours is 24 * fraction of SYSDATE - datemet
		*/	
		, TRUNC
			(
				24 * 
					( SYSDATE - dateMet )
					
			) AS "numberofHoursWhole"
		
FROM   friendBook tblFB

ORDER BY
	
		tblFB.dateMet ASC 
		
;



TRUNCATE TABLE friendBook;

DROP TABLE friendBook;

COMMIT;



Result

Grid

Image

Textual
friend DateMet nowUsingToChar nowUsingToCharFormatted numberofDaysFractional numberofDaysFractional ( 3 dec. places ) numberofDaysWhole numberofHoursWhole
Bob Wood 2023-01-01 00:00:00.000 07-FEB-23 2023-02-07 10:04 pm 37.91957175925925925925925925925925925926 37.92 37 910
Steve Winwood 2023-02-01 00:00:00.000 07-FEB-23 2023-02-07 10:04 pm 6.91957175925925925925925925925925925926 6.92 6 166
Terry Bradshaw 2023-02-07 00:00:00.000 07-FEB-23 2023-02-07 10:04 pm 0.9195717592592592592592592592592592592593 0.92 0 22
Sam Niel 2023-02-07 22:04:11.000 07-FEB-23 2023-02-07 10:04 pm 0 0 0 0

Code Sharing

GitHub

Gist

DanielAdeniji/OracleDataDiffFriendBook.Link

 

Dedication

Borland Delphi

BTW, learnt how to calculate date differences using date arithmetic from Borland’s Delphi.

As such I am going to dedicate this post to the Borland Delphi Team.

Inclusive:-

  1. Philippe Kahn
  2. Anders Hejlsberg
  3. Danny Thorpe
  4. Lance Devin ( My Brother Lance )

 

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s