Using two date-and-time values that lie a week apart : TO_DAYS « Date Time « SQL / MySQL






Using two date-and-time values that lie a week apart

     
mysql>
mysql> SET @dt1 = '1800-02-14 07:30:00';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @dt2 = '1800-02-21 07:30:00';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @interval = ((TO_DAYS(@dt2) - TO_DAYS(@dt1)) * 24*60*60) + TIME_TO_SEC(@dt2) - TIME_TO_SEC(@dt1);
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @interval AS seconds;
+---------+
| seconds |
+---------+
|  604800 |
+---------+
1 row in set (0.00 sec)

mysql>

   
    
    
    
    
  








Related examples in the same category

1.TO_DAYS( ) can convert DATETIME or TIMESTAMP values to days, if you don't mind having it chop off the time par
2.If you pass TO_DAYS( ) a date-and-time value, it extracts the date part and discards the time.
3.TO_DAYS( ) converts a date to the corresponding number of days
4.Calculating Intervals Between Dates
5.For an interval in weeks, do the same thing and divide the result by seven