Oracle和MySQL的常用函数比较(二)

上一节我们主要比较和学习了字符串函数,本节我们来比较和学习应用广泛的日期函数。

日期函数

  • 当前日期和时间(current date ,current time):
  • Oracle
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

Session altered.

SQL> select current_date from dual;

CURRENT_DATE
-------------------
2019-07-30 12:03:10


SQL> select current_timestamp from dual;

CURRENT_TIMESTAMP
---------------------------------------------------------------------------
30-JUL-19 09.58.07.263945 AM +08:00

SQL> 
SQL> select sysdate from dual;

SYSDATE
-------------------
2019-07-30 11:36:18

SQL> select systimestamp from dual;

SYSTIMESTAMP
---------------------------------------------------------------------------
30-JUL-19 11.36.28.261302 AM +08:00

SQL> 

  • MySQL
mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2019-07-30 |
+------------+
1 row in set (0.00 sec)

mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 09:59:49  |
+-----------+
1 row in set (0.00 sec)

mysql> 
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2019-07-30 09:55:59 |
+---------------------+
1 row in set (0.00 sec)

  • 日期提取(extract):
  • Oracle
SQL> select extract(day from sysdate) from dual;

EXTRACT(DAYFROMSYSDATE)
-----------------------
                     30

SQL> select extract(month from sysdate) from dual;

EXTRACT(MONTHFROMSYSDATE)
-------------------------
                        7

SQL> 
  • MySQL
mysql> select extract(day from now());
+-------------------------+
| extract(day from now()) |
+-------------------------+
|                      30 |
+-------------------------+
1 row in set (0.00 sec)

mysql> select extract(month from now());
+---------------------------+
| extract(month from now()) |
+---------------------------+
|                         7 |
+---------------------------+
1 row in set (0.00 sec)

mysql> 
  • 日期加法(add_months,date_add):
  • Oracle
SQL> select add_months(date '2016-10-01',1) from dual;

ADD_MONTHS(DATE'201
-------------------
2016-11-01 00:00:00


SQL> select date '2016-10-01'+1 from dual;

DATE'2016-10-01'+1
-------------------
2016-10-02 00:00:00


SQL>  select date '2016-10-01'+365 from dual;

DATE'2016-10-01'+36
-------------------
2017-10-01 00:00:00

SQL> 

SQL> select timestamp '2016-10-01 01:02:03'+1/24 from dual;

TIMESTAMP'2016-10-0
-------------------
2016-10-01 02:02:03

SQL> select timestamp '2016-10-01 01:02:03'+1/24/60 from dual;

TIMESTAMP'2016-10-0
-------------------
2016-10-01 01:03:03

SQL> select timestamp '2016-10-01 01:02:03'+1/24/60/60 from dual;

TIMESTAMP'2016-10-0
-------------------
2016-10-01 01:02:04

SQL> 
  • MySQL
mysql> select date_add('2016-01-01',interval 1 day) ;
+---------------------------------------+
| date_add('2016-01-01',interval 1 day) |
+---------------------------------------+
| 2016-01-02                            |
+---------------------------------------+
1 row in set (0.00 sec)

mysql> select date_add('2016-01-01',interval 1 month) ;
+-----------------------------------------+
| date_add('2016-01-01',interval 1 month) |
+-----------------------------------------+
| 2016-02-01                              |
+-----------------------------------------+
1 row in set (0.00 sec)

mysql> select date_add('2016-01-01',interval 1 year) ;
+----------------------------------------+
| date_add('2016-01-01',interval 1 year) |
+----------------------------------------+
| 2017-01-01                             |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> select date_add('2016-01-01 02:03:04',interval 1 hour);
+-------------------------------------------------+
| date_add('2016-01-01 02:03:04',interval 1 hour) |
+-------------------------------------------------+
| 2016-01-01 03:03:04                             |
+-------------------------------------------------+
1 row in set (0.00 sec)

mysql> select date_add('2016-01-01 02:03:04',interval 1 minute);
+---------------------------------------------------+
| date_add('2016-01-01 02:03:04',interval 1 minute) |
+---------------------------------------------------+
| 2016-01-01 02:04:04                               |
+---------------------------------------------------+
1 row in set (0.00 sec)

mysql> select date_add('2016-01-01 02:03:04',interval 1 second);
+---------------------------------------------------+
| date_add('2016-01-01 02:03:04',interval 1 second) |
+---------------------------------------------------+
| 2016-01-01 02:03:05                               |
+---------------------------------------------------+
1 row in set (0.00 sec)

mysql> select date_add('2016-01-01 02:03:04',interval '1-1' year_month);
+-----------------------------------------------------------+
| date_add('2016-01-01 02:03:04',interval '1-1' year_month) |
+-----------------------------------------------------------+
| 2017-02-01 02:03:04                                       |
+-----------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select date_add('2016-01-01 02:03:04',interval '1 1' day_hour);
+---------------------------------------------------------+
| date_add('2016-01-01 02:03:04',interval '1 1' day_hour) |
+---------------------------------------------------------+
| 2016-01-02 03:03:04                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select date_add('2016-01-01 02:03:04',interval '1 1:1' day_minute);
+-------------------------------------------------------------+
| date_add('2016-01-01 02:03:04',interval '1 1:1' day_minute) |
+-------------------------------------------------------------+
| 2016-01-02 03:04:04                                         |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select date_add('2016-01-01 02:03:04',interval '1:1' hour_minute);
+------------------------------------------------------------+
| date_add('2016-01-01 02:03:04',interval '1:1' hour_minute) |
+------------------------------------------------------------+
| 2016-01-01 03:04:04                                        |
+------------------------------------------------------------+
1 row in set (0.01 sec)

mysql> 
  • 日期差值(diff):
  • Oracle
SQL> select months_between(date '2016-10-01', date '2016-11-01') from dual;

MONTHS_BETWEEN(DATE'2016-10-01',DATE'2016-11-01')
-------------------------------------------------
                                               -1

SQL> select timestamp '2016-10-01 01:02:03' - timestamp '2016-11-01 01:02:03' from dual;

TIMESTAMP'2016-10-0101:02:03'-TIMESTAMP'2016-11-0101:02:03'
---------------------------------------------------------------------------
-000000031 00:00:00.000000000

SQL> select timestamp '2016-10-01 01:02:03' - timestamp '2016-11-01 02:03:04' from dual;

TIMESTAMP'2016-10-0101:02:03'-TIMESTAMP'2016-11-0102:03:04'
---------------------------------------------------------------------------
-000000031 01:01:01.000000000

SQL> 
  • MySQL
mysql> select datediff('2016-01-01','2016-02-01');
+-------------------------------------+
| datediff('2016-01-01','2016-02-01') |
+-------------------------------------+
|                                 -31 |
+-------------------------------------+
1 row in set (0.00 sec)

mysql> select timediff('2016-01-01 01:02:03','2016-01-02 02:03:04');
+-------------------------------------------------------+
| timediff('2016-01-01 01:02:03','2016-01-02 02:03:04') |
+-------------------------------------------------------+
| -25:01:01                                             |
+-------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select timestampdiff(hour,'2016-10-01 01:02:03','2016-10-01 02:03:04');
+-----------------------------------------------------------------+
| timestampdiff(hour,'2016-10-01 01:02:03','2016-10-01 02:03:04') |
+-----------------------------------------------------------------+
|                                                               1 |
+-----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select timestampdiff(minute,'2016-10-01 01:02:03','2016-10-01 02:03:04');
+-------------------------------------------------------------------+
| timestampdiff(minute,'2016-10-01 01:02:03','2016-10-01 02:03:04') |
+-------------------------------------------------------------------+
|                                                                61 |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select timestampdiff(second,'2016-10-01 01:02:03','2016-10-01 02:03:04');
+-------------------------------------------------------------------+
| timestampdiff(second,'2016-10-01 01:02:03','2016-10-01 02:03:04') |
+-------------------------------------------------------------------+
|                                                              3661 |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)

  • 日期取整,截取(round,trunc):
  • Oracle
SQL> select round(timestamp '2016-10-01 01:02:03','yy') from dual;

ROUND(TIMESTAMP'201
-------------------
2017-01-01 00:00:00

SQL>  select round(timestamp '2016-10-01 01:02:03','mm') from dual;

ROUND(TIMESTAMP'201
-------------------
2016-10-01 00:00:00

SQL> select round(timestamp '2016-10-16 01:02:03','mm') from dual;

ROUND(TIMESTAMP'201
-------------------
2016-11-01 00:00:00

SQL> select round(timestamp '2016-10-01 01:02:03','dd') from dual;

ROUND(TIMESTAMP'201
-------------------
2016-10-01 00:00:00

SQL> select round(timestamp '2016-10-01 13:02:03','dd') from dual;

ROUND(TIMESTAMP'201
-------------------
2016-10-02 00:00:00

SQL> select trunc(date '2016-02-03','yy') from dual;

TRUNC(DATE'2016-02-
-------------------
2016-01-01 00:00:00

SQL> select trunc(date '2016-02-03','mm') from dual;

TRUNC(DATE'2016-02-
-------------------
2016-02-01 00:00:00

SQL> select trunc(date '2016-02-03','dd') from dual;

TRUNC(DATE'2016-02-
-------------------
2016-02-03 00:00:00

  • MySQL

你可能感兴趣的:(Oracle和MySQL的常用函数比较(二))