3.DB2 时间函数

 

1.获取当前时间

(1) CURRENT DATE

获取当前的日期

1
2
SELECT CURRENT DATE AS "CURRENT_DATE" FROM AP_INFO;
-- 2023-02-01

(2) CURRENT TIME

获取当前的时间

1
2
SELECT CURRENT TIME AS "CURRENT_TIME" FROM AP_INFO;
-- 10:55:25

(3) CURRENT TIMESTAMP

获取当前的时间戳

1
2
SELECT CURRENT TIMESTAMP AS "CURRENT_TIMESTAMP" FROM AP_INFO;
-- 2023-02-01 10:55:25.517679

2.获取时间信息

获取年份

1
2
SELECT YEAR(CHANGE_TIME) AS "CHANGE YEAR" FROM AP_INFO;
-- 2023

获取月份

1
2
SELECT MONTH(CHANGE_TIME) AS "CHANGE MONTH" FROM AP_INFO;
-- 3

获取日

1
2
SELECT DAY(CHANGE_TIME) AS "CHANGE DAY" FROM AP_INFO;
-- 10

获取小时(24H)

1
2
SELECT HOUR(CHANGE_TIME) AS "CHANGE HOUR" FROM AP_INFO;
-- 15

获取分钟

1
2
SELECT MINUTE(CHANGE_TIME) AS "CHANGE MINUTE" FROM AP_INFO;
-- 29

获取秒

1
2
SELECT SECOND(CHANGE_TIME) AS "CHANGE SECOND" FROM AP_INFO;
-- 29

3.计算两个日期相差多少天

DAYS([DATE]): 返回日期从 0001-01-01 以来的天数

1
2
SELECT (DAYS(CURRENT DATE) - DAYS(CHANGE_TIME)) AS "BETWEEN DAYS" FROM AP_INFO;
-- 3