mysql> SELECT TIMESTAMPADD(MINUTE,1,'2019-03-15'); +---------------------------------------------------------+ | TIMESTAMPADD(MINUTE,1,'2019-03-15') | +---------------------------------------------------------+ | 2019-03-15 00:01:00 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
Function syntax of the form: TIMESTAMPDIFF (unit, datetime_expr1, datetime_expr2).
The SQL TIMESTAMPDIFF () function returns an integer representing the time difference between two expressions datetime_expr1 and datetime_expr2.
mysql> SELECT TIMESTAMPDIFF(MONTH,'2019-02-01','2019-05-01'); +---------------------------------------------------------+ | TIMESTAMPDIFF(MONTH,'2019-02-01','2019-05-01') | +---------------------------------------------------------+ | 3 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
Function syntax is of the form: TIME_FORMAT (time, format).
The TIME_FORMAT function in SQL is used as the DATE_FORMAT () function, but the format parameter may contain only the hour, minute, and second format.
Specifier Explain % f microsecond (000000.999999)% H Hour, value form (00.23)% h Hour, value form (01.12)% I Hour, value form (01.12) )% i Minutes, numeric value form (00.59)% p AM or PM% r Time, 12h (hh: mm: ss is followed by AM or PM)% S Seconds (00.59)% s Seconds (00.59)% T Time, 24h (hh: mm: ss)For example :
mysql> SELECT TIME_FORMAT('19:30:10', '%h %i %s %p'); +---------------------------------------------------------+ | TIME_FORMAT('19:30:10', '%h %i %s %p') | +---------------------------------------------------------+ | 07 30 10 PM | +---------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> SELECT TIME_FORMAT('19:30:10', '%r'); +---------------------------------------------------------+ | TIME_FORMAT('19:30:10', '%r') | +---------------------------------------------------------+ | 07:30:10 PM | +---------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> SELECT TIME_FORMAT('97:15:40','%H %k %h %I %l'); +---------------------------------------------------------+ | TIME_FORMAT('97:15:40','%H %k %h %I %l') | +---------------------------------------------------------+ | 97 97 01 01 1 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
If the time value contains a part of time greater than 23, we use % H and % k to return a value greater than the range of values normally used 0 . 23.
The TIME_TO_SEC () function in SQL is in the form TIME_TO_SEC (time) , returning the number of seconds converted from the time parameter .
mysql> SELECT TIME_TO_SEC('22:23:00'); +---------------------------------------------------------+ | TIME_TO_SEC('22:23:00') | +---------------------------------------------------------+ | 80580 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
TO_DAYS () function in SQL has the form TO_DAYS (date), returns the number of days between date value and year 0 (calculated from 0000-00-00).
mysql> SELECT TO_DAYS('2019-03-15'); +---------------------------------------------------------+ | TO_DAYS('2019-03-15') | +---------------------------------------------------------+ | 737498 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> SELECT TO_DAYS(190316); +---------------------------------------------------------+ | TO_DAYS(190316) | +---------------------------------------------------------+ | 737499 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
There are two types of UNIX_TIMESTAMP functions:
1. UNIX_TIMESTAMP ()
In SQL, if the UNIX_TIMESTAMP () function does not have an associated parameter, it returns the number of seconds according to Unix timestamp, ie the number of seconds since '1970-01-01 00:00:00' UTC to the current date and time in numeric form. untouched.
mysql> SELECT UNIX_TIMESTAMP(); +---------------------------------------------------------+ | UNIX_TIMESTAMP() | +---------------------------------------------------------+ | 1552677612 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
2. UNIX_TIMESTAMP (date)
If the UNIX_TIMESTAMP function has the following date parameter, SQL will return the number of seconds according to the Unix timestamp from '1970-01-01 00:00:00' UTC to the date and time value passed as an unsigned integer (date has can be a string of DATE, DATETIME, TIMESTAMP, or a number in YYMMDD or YYYYMMDD format .
mysql> SELECT UNIX_TIMESTAMP('1995-02-12 22:23:00'); +---------------------------------------------------------+ | UNIX_TIMESTAMP('1995-02-12 22:23:00') | +---------------------------------------------------------+ | 792627780 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
The function UTC_DATE () or UTC_DATE in SQL returns the current UTC date as a value in the format 'YYYY-MM-DD' or YYYYMMDD, depending on whether the function is being used in the string or numeric context.
mysql> SELECT UTC_DATE(); +---------------------------------------------------------+ | UTC_DATE() | +---------------------------------------------------------+ | 2019-03-15 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> SELECT UTC_DATE() + 0; +---------------------------------------------------------+ | UTC_DATE() + 0 | +---------------------------------------------------------+ | 20190315 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
The UTC_TIME () or UTC_TIME function returns the current UTC time as a value formatted as 'HH: MM: SS' or HHMMSS, depending on whether the function is being used in the string or numeric context.
mysql> SELECT UTC_TIME(); +---------------------------------------------------------+ | UTC_TIME() | +---------------------------------------------------------+ | 18:07:53 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> SELECT UTC_TIME() + 0; +---------------------------------------------------------+ | UTC_TIME() + 0 | +---------------------------------------------------------+ | 180753 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
The UTC_TIMESTAMP () or UTC_TIMESTAMP function returns the current UTC date and time as a value in the format ' YYYY-MM-DD HH: MM: SS ' or YYYYMMDDHHMMSS, depending on whether the function is being used in a good string context number.
mysql> SELECT UTC_TIMESTAMP(); +---------------------------------------------------------+ | UTC_TIMESTAMP() | +---------------------------------------------------------+ | 2019-03-15 18:08:04 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> SELECT UTC_TIMESTAMP() + 0; +---------------------------------------------------------+ | UTC_TIMESTAMP() + 0 | +---------------------------------------------------------+ | 20190315180804 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
WEEKDAY () in SQL is of the form: WEEKDAY (date).
The WEEKDAY () function returns the weekday index (0 = Monday, 1 = Tuesday . 6 = Sunday).
mysql> SELECT WEEKDAY('2019-03-15 22:23:00'); +---------------------------------------------------------+ | WEEKDAY('2019-03-15 22:23:00') | +---------------------------------------------------------+ | 4 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
WEEKOFYEAR () in SQL takes the form: WEEKOFYEAR (date).
The WEEKOFYEAR () function returns only the week number of the date of the year that is an integer between 1 and 53.
mysql> SELECT WEEKOFYEAR('2019-03-15'); +---------------------------------------------------------+ | WEEKOFYEAR('2019-03-15') | +---------------------------------------------------------+ | 11 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
The YEAR () function in SQL has the form YEAR (date), returning the corresponding year of the passed date parameter. The results are in the range 1000 to 9999.
mysql> SELECT YEAR('19-03-15'); +---------------------------------------------------------+ | YEAR('19-03-15') | +---------------------------------------------------------+ | 2019 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
In the next section, we will learn about the Temporary Table temporary table in SQL , please keep track.
Previous article: Function handling DATE / TIME in SQL - Part 1
Next article: Temporary Table temporary table in SQL