SQL time difference function timediff, timestampdiff, datediff

Time difference function:

timestampdiff

语法:timestampdiff(interval, datetime1,datetime2)

Results: Returns the time difference (time period 2- 1), the results given by the unit interval parameter.

  • frac_second ms (low version is not supported by second, and then divided by 1000)
  • second seconds
  • minute minute
  • hour hour
  • day of days
  • week week
  • month month
  • quarter quarter
  • year in

  Note: MySQL only supports recording and computing milliseconds after 5.6, if a previous version, preferably in a database other than datetime field types, then create a field for storing int milliseconds, and then converted his own calculations.

# All formats
 the SELECT TIMESTAMPDIFF (FRAC_SECOND, ' 2012-10-01 ' , ' 2013-01-13 ' ); # does not support
 the SELECT TIMESTAMPDIFF (SECOND,, ' 2012-10-01 ' , ' 2013-01-13 ' ) ; # 8.9856 million 
the SELECT TIMESTAMPDIFF (MINUTE, ' 2012-10-01 ' , ' 2013-01-13 ' ); # 149760 
the SELECT TIMESTAMPDIFF (HOUR, ' 2012-10-01 ' , ' 2013-01-13 ' );# 2496
SELECT TIMESTAMPDIFF(DAY,'2012-10-01','2013-01-13'); # 104
SELECT TIMESTAMPDIFF(WEEK,'2012-10-01','2013-01-13'); # 14
SELECT TIMESTAMPDIFF(MONTH,'2012-10-01','2013-01-13'); # 3
SELECT TIMESTAMPDIFF(QUARTER,'2012-10-01','2013-01-13'); # 1
SELECT TIMESTAMPDIFF(YEAR,'2012-10-01','2013-01-13'); # 0

 

 

datediff

Syntax: pass two date parameters, compare DAY days, minus the first parameter of the second parameter values day

SELECT DATEDIFF('2013-01-13','2012-10-01');# 104

timediff

Syntax: timeDiff (TIME1, TIME2)

Results: Returns two time difference obtained by subtracting, time1-time2

SELECT TIMEDIFF('2018-05-21 14:51:43','2018-05-19 12:54:43');# 49:57:00

 

 

Other date functions

  • now () function returns the date when the current time every minute
  • curdate () function returns the date information
  • curtime () function returns the current time information of minutes and seconds
  • When a date to the date contained Minutes formatted date date, use DATE (time) function
    # Other date functions
     the SELECT the NOW (); # 2018 - 05 - 21 is  14 : 41 is : 00 
    the SELECT CURDATE (); # 2018 - 05 - 21 is 
    the SELECT CURTIME (); # 14 : 41 is : 38 is 
    the SELECT DATE (the NOW ()); # 2018 - 05 - 21 is 
    the SELECT SYSDATE (); # 2018 - 05 - 21 is  14 : 47 : . 11 
    the SELECT  CURRENT_TIME (); # 14:51:30
    SELECT CURRENT_TIMESTAMP; # 2018-05-21 14:51:37
    SELECT CURRENT_TIMESTAMP(); # 2018-05-21 14:51:43

    Note: now () and sysdate () is similar, except now () will get the start in the implementation, but sysdate () can be obtained dynamically when the function execution.

 

Guess you like

Origin www.cnblogs.com/87060524test/p/11647976.html