1>获取当前日期的函数:CURDATE(),CURRENT_DATE(),CURDATE()+0
mysql> SELECT CURDATE(),CURRENT_DATE(),CURDATE()+0;
+------------+----------------+-------------+
| CURDATE() | CURRENT_DATE() | CURDATE()+0 |
+------------+----------------+-------------+
| 2017-07-31 | 2017-07-31 | 20170731 |
+------------+----------------+-------------+
1 row in set (0.03 sec)
获取当前时间的函数CURTIME(),CURRENT_TIME()
mysql> SELECT CURRENT_time(),curtime();
+----------------+-----------+
| CURRENT_time() | curtime() |
+----------------+-----------+
| 16:54:19 | 16:54:19 |
+----------------+-----------+
1 row in set (0.00 sec)
获取当前日期和时间的函数:CURRENT_TIMESTAMP(),LOCALTIME(),NOW(),SYSDATE()
mysql> SELECT CURRENT_TIMESTAMP(),LOCALTIME(),NOW(),SYSDATE();
+---------------------+---------------------+---------------------+-------------
--------+
| CURRENT_TIMESTAMP() | LOCALTIME() | NOW() | SYSDATE()
|
+---------------------+---------------------+---------------------+-------------
--------+
| 2017-07-31 16:51:57 | 2017-07-31 16:51:57 | 2017-07-31 16:51:57 | 2017-07-31 1
6:51:57 |
+---------------------+---------------------+---------------------+-------------
--------+
1 row in set (0.04 sec)
2>返回UTC日期的函数UTC_DATE()
mysql> SELECT UTC_DATE();
+------------+
| UTC_DATE() |
+------------+
| 2017-07-31 |
+------------+
1 row in set (0.00 sec)
返回UTC时间的函数UTC_TIME()
mysql> SELECT UTC_TIME();
+------------+
| UTC_TIME() |
+------------+
| 08:58:54 |
+------------+
1 row in set (0.00 sec)
3>获取月份的函数MONTH(date)和MONTHNAME(date)
mysql> select month('2017-5-18'),monthname('2017-5-18');
+--------------------+------------------------+
| month('2017-5-18') | monthname('2017-5-18') |
+--------------------+------------------------+
| 5 | May |
+--------------------+------------------------+
1 row in set (0.02 sec)
4>获取星期的函数DAYNAME(d)、DAYOFWEEK(d)、WEEKDAY(d)
mysql> select dayname('2017-7-31'),dayofweek('2017-7-31'),weekday('2017-7-31');
+----------------------+------------------------+----------------------+
| dayname('2017-7-31') | dayofweek('2017-7-31') | weekday('2017-7-31') |
+----------------------+------------------------+----------------------+
| Monday | 2 | 0 |
+----------------------+------------------------+----------------------+
1 row in set (0.10 sec)
5>获取星期数的函数WEEK(d)和WEEKOFYEAR(d)
mysql> select week('2017-02-20',1),weekofyear('2017-02-20');
+----------------------+--------------- -----------+
| week('2017-02-20',1) | weekofyear('2017-02-20') |
+----------------------+--------------------------+
| 8 | 8 |
+----------------------+--------------------------+
1 row in set (0.00 sec)
6>获取天数的函数DAYOFYEAR(d)和DAYOFMONTH(d)
mysql> select dayofyear('2017-02-20'),dayofmonth('2017-07-30');
+-------------------------+--------------------------+
| dayofyear('2017-02-20') | dayofmonth('2017-07-30') |
+-------------------------+--------------------------+
| 51 | 30 |
+-------------------------+--------------------------+
1 row in set (0.04 sec)
7>获取年份、季度、小时、分钟、秒钟的函数
mysql> select year('2017-7-31'),quarter('17-7-31'),minute('17-7-31 17:23:13'),se
cond('10:03:50');
+-------------------+--------------------+----------------------------+---------
-----------+
| year('2017-7-31') | quarter('17-7-31') | minute('17-7-31 17:23:13') | second('
10:03:50') |
+-------------------+--------------------+----------------------------+---------
-----------+
| 2017 | 3 | 23 |
50 |
+-------------------+--------------------+----------------------------+---------
-----------+
1 row in set (0.00 sec)
8>获取如期指定值的函数EXTRACT(type FROM date)
mysql> select extract(year from '2017-04-15');
+---------------------------------+
| extract(year from '2017-04-15') |
+---------------------------------+
| 2017 |
+---------------------------------+
1 row in set (0.17 sec)
mysql> select extract(year_month from '2017-04-15');
+---------------------------------------+
| extract(year_month from '2017-04-15') |
+---------------------------------------+
| 201704 |
+---------------------------------------+
1 row in set (0.00 sec)
9>时钟和秒钟转换的函数
mysql> select time_to_sec('23:23:00');
+-------------------------+
| time_to_sec('23:23:00') |
+-------------------------+
| 84180 |
+-------------------------+
1 row in set (0.00 sec)
mysql> select sec_to_time(84180);
+--------------------+
| sec_to_time(84180) |
+--------------------+
| 23:23:00 |
+--------------------+
1 row in set (0.02 sec)
10>计算日期和时间的函数
mysql> SELECT date_add('2017-12-31 23:59:59', interval 1 second);
+----------------------------------------------------+
| date_add('2017-12-31 23:59:59', interval 1 second) |
+----------------------------------------------------+
| 2018-01-01 00:00:00 |
+----------------------------------------------------+
1 row in set (0.05 sec)
mysql> SELECT date_add('2017-12-31 23:59:59', interval '1:1' minute_second);
+---------------------------------------------------------------+
| date_add('2017-12-31 23:59:59', interval '1:1' minute_second) |
+---------------------------------------------------------------+
| 2018-01-01 00:01:00 |
+---------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT adddate('2017-12-31 23:59:59', interval '1:1' minute_second);
+--------------------------------------------------------------+
| adddate('2017-12-31 23:59:59', interval '1:1' minute_second) |
+--------------------------------------------------------------+
| 2018-01-01 00:01:00 |
+--------------------------------------------------------------+
1 row in set (0.02 sec)
mysql> select date_sub('2017-7-31 00:01:00',interval '0 0:1:1' day_second);
+--------------------------------------------------------------+
| date_sub('2017-7-31 00:01:00',interval '0 0:1:1' day_second) |
+--------------------------------------------------------------+
| 2017-07-30 23:59:59 |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_sub('2017-7-31',interval 30 day);
+---------------------------------------+
| date_sub('2017-7-31',interval 30 day) |
+---------------------------------------+
| 2017-07-01 |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> select subdate('2017-7-31',interval 30 day);
+--------------------------------------+
| subdate('2017-7-31',interval 30 day) |
+--------------------------------------+
| 2017-07-01 |
+--------------------------------------+
1 row in set (0.00 sec)
mysql> select addtime('2017-7-31 4:4:4','1:1:1');
+------------------------------------+
| addtime('2017-7-31 4:4:4','1:1:1') |
+------------------------------------+
| 2017-07-31 05:05:05 |
+------------------------------------+
1 row in set (0.04 sec)
mysql> select subtime('2017-7-31 4:4:4','1:1:1');
+------------------------------------+
| subtime('2017-7-31 4:4:4','1:1:1') |
+------------------------------------+
| 2017-07-31 03:03:03 |
+------------------------------------+
1 row in set (0.01 sec)
mysql> select datediff('2017-7-30 2:2:2','2017-7-28');
+-----------------------------------------+
| datediff('2017-7-30 2:2:2','2017-7-28') |
+-----------------------------------------+
| 2 |
+-----------------------------------------+
1 row in set (0.04 sec)
11>将日期和时间格式化的函数
DATE_FORMAT()函数格式化输出日期和时间值。
mysql> select date_format('2017-7-30 12:00:00','%w %m %y');
+----------------------------------------------+
| date_format('2017-7-30 12:00:00','%w %m %y') |
+----------------------------------------------+
| 0 07 17 |
+----------------------------------------------+
1 row in set (0.00 sec)
mysql> select time_format('9:02:10','%H %k %h %I %l');
+-----------------------------------------+
| time_format('9:02:10','%H %k %h %I %l') |
+-----------------------------------------+
| 09 9 09 09 9 |
+-----------------------------------------+
1 row in set (0.00 sec)
mysql> select get_format(date,'EUR');
+------------------------+
| get_format(date,'EUR') |
+------------------------+
| %d.%m.%Y |
+------------------------+
1 row in set (0.00 sec)