loading..
Русский    English
16:02

DATEDIFF function page 2

Exercise. Count the number of minutes in the interval between the two dates '2011-10-07 23:43:00' and '2011-10-08 01:23:00'


SQL Server

The exercise can be solved with built-in function DATEDIFF:

Console
Execute
  1. SELECT DATEDIFF (minute, '2011-10-07T23:43:00', '2011-10-08T01:23:00');

The answer is 100 minutes.

Notes:

The query uses the ISO standard date notation as a text string 'yyyy-mm-ddThh:mm:ss'. The notation is interpreted unambiguously by any DBMS brand with any database server settings.


MySQL

MySQL also supports DATEDIFF function, but it has a different meaning. It takes two dates as arguments and computes the number of days between them. If a date is written in date-time format, only date component is used. That is why all the following queries produce the same result –1. The result will be positive if the first argument is larger than the second one.

  1. SELECT DATEDIFF('2011-10-07T23:43:00', '2011-10-08T01:23:00');
  2. SELECT DATEDIFF('2011-10-07', '2011-10-08');
  3. SELECT DATEDIFF('2011-10-07T23:43:00', '2011-10-08');

The result “1 day” would be produced even if the dates differ with one second:

  1. SELECT DATEDIFF('2011-10-07T23:59:59', '2011-10-08T00:00:00');

It is another built-in function TIMESTAMPDIFF that solves the problem. This function is an analogue of SQL Server DATEDIFF:

  1. SELECT TIMESTAMPDIFF(minute, '2011-10-07T23:43:00', '2011-10-08T01:23:00');

PostgreSQL

PostgreSQL has no function analogous to DATEDIFF in SQL Server nor to TIMESTAMPDIFF in MySQL. It is possible to act the following way to solve the exercise:

  1. to present the difference between two dates as a time interval;
  2. to count the number of seconds in the interval;
  3. to divide the value by 60.

To get the time interval one can take a difference of two temporal values. An explicit type conversion is required in this case:

  1. SELECT timestamp '2011-10-08T01:23:00' - timestamp '2011-10-07T23:43:00';
or in standard notation:

  1. SELECT CAST('2011-10-08T01:23:00' AS timestamp) - CAST('2011-10-07T23:43:00' AS timestamp);

The result is "01:40:00", none other than the duration of one hour and forty minutes.

A built-in function AGE produces the same result. The function performs its arguments type conversion implicitly:

  1. SELECT AGE ('2011-10-08T01:23:00', '2011-10-07T23:43:00');

The following function can be used to get the number of seconds in the time interval:

  1. EXTRACT(EPOCH FROM < interval >)

The number of seconds must be divided by 60. So the final result can be produced with the following statement:

  1. SELECT EXTRACT(EPOCH FROM AGE('2011-10-08T01:23:00', '2011-10-07T23:43:00'))/60;

In this case the result is an integer number because both of the time values contain zero second values. If values of seconds are greater than zero, a decimal number may be produced. For example the following statement produces 99.75:

  1. SELECT EXTRACT(EPOCH FROM AGE ('2011-10-08T01:23:00', '2011-10-07T23:43:15'))/60;

Bookmark and Share
Pages 1 2
The book was updated
several days ago
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.
Rambler's Top100