南大通用GBase 8a获得当前日期时间毫秒微秒的几个函数使用样例

本文介绍GBase 8a数据库集群里,获得当前日期,时间,毫秒微秒的几个函数的使用方法。

汇总

其中sysdate是兼容oracle的写法,但需要有函数的小括号。current_date,current_date()都可以

函数小括号日期时间毫秒微秒
now必须YY
current_dateY
current_datetimeYYY
current_timestampYY
current_timeY
utc_dateY
utc_datetimeYYY
utc_timestampYY
utc_timeY
sysdate必须YY
MICROSECOND必须Y

now

返回当前日期时间

gbase> select now();
+---------------------+
| now()               |
+---------------------+
| 2021-01-07 17:05:27 |
+---------------------+
1 row in set (Elapsed: 00:00:00.00)

current_date

当前日期,不包含时间。

gbase> select current_date;
+--------------+
| current_date |
+--------------+
| 2021-01-07   |
+--------------+
1 row in set (Elapsed: 00:00:00.01)

gbase> select current_date();
+----------------+
| current_date() |
+----------------+
| 2021-01-07     |
+----------------+
1 row in set (Elapsed: 00:00:00.00)

current_datetime

当前日期时间,包含毫秒,微秒。

gbase> select current_datetime;
+----------------------------+
| current_datetime           |
+----------------------------+
| 2021-01-07 17:30:03.268713 |
+----------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select current_datetime();
+----------------------------+
| current_datetime()         |
+----------------------------+
| 2021-01-07 17:06:29.661921 |
+----------------------------+
1 row in set (Elapsed: 00:00:00.00)

current_timestamp

当前时间戳,日期和时间。

gbase> select current_timestamp;
+---------------------+
| current_timestamp   |
+---------------------+
| 2021-01-07 17:28:29 |
+---------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2021-01-07 17:08:58 |
+---------------------+
1 row in set (Elapsed: 00:00:00.00)

current_time

当前时间。

gbase> select current_time;
+--------------+
| current_time |
+--------------+
| 17:29:20     |
+--------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select current_time();
+----------------+
| current_time() |
+----------------+
| 17:09:03       |
+----------------+
1 row in set (Elapsed: 00:00:00.00)

MICROSECOND(expr)

以数字的形式返回 time 或者 datetime 表达式 expr 中的微秒值,数字取值范围是 0到 999999。

gbase> select microsecond('2021-02-03 13:14:15.123456');
+-------------------------------------------+
| microsecond('2021-02-03 13:14:15.123456') |
+-------------------------------------------+
|                                    123456 |
+-------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)

sysdate

当前的日期和时间。

gbase> select sysdate();
+---------------------+
| sysdate()           |
+---------------------+
| 2021-01-07 17:09:33 |
+---------------------+
1 row in set (Elapsed: 00:00:00.00)

utc_date

获取当前UTC的日期,不包括时间。

gbase> select utc_date,utc_date();
+------------+------------+
| utc_date   | utc_date() |
+------------+------------+
| 2021-01-07 | 2021-01-07 |
+------------+------------+
1 row in set (Elapsed: 00:00:00.00)

utc_datetime

获取当前UTC的日期和时间,包括毫秒微秒。

gbase> select utc_datetime,utc_datetime();
+----------------------------+----------------------------+
| utc_datetime               | utc_datetime()             |
+----------------------------+----------------------------+
| 2021-01-07 09:27:43.412631 | 2021-01-07 09:27:43.412631 |
+----------------------------+----------------------------+
1 row in set (Elapsed: 00:00:00.00)

utc_time

获取当前UTC的时间,不包括日期。

gbase> select utc_time();
+------------+
gbase> select utc_time,utc_time();
+----------+------------+
| utc_time | utc_time() |
+----------+------------+
| 09:27:23 | 09:27:23   |
+----------+------------+
1 row in set (Elapsed: 00:00:00.01)

utc_timestamp

获取当前UTC的时间和日期。无毫秒微秒。

gbase> select utc_timestamp;
+---------------------+
| utc_timestamp       |
+---------------------+
| 2021-01-07 09:36:14 |
+---------------------+
1 row in set (Elapsed: 00:00:00.01)

gbase> select utc_timestamp();
+---------------------+
| utc_timestamp()     |
+---------------------+
| 2021-01-07 09:36:46 |
+---------------------+
1 row in set (Elapsed: 00:00:00.00)

参考