db2取資料庫日期時間 DB2日期和時間函式彙總

2021-10-17 05:11:42 字數 4010 閱讀 6419

1.current date獲取當前日期;current time獲取當前時間;current timestamp獲取當前時間戳(含年、月、日、時、分、秒);year()獲取年;month()獲取月;day()獲取日; hour()獲取小時;minute()獲取分鐘;second()獲取秒;date()獲取日期;time()獲取時間;timestamp()獲取時間戳。

以上函式引數可以是日期時間型別,也可以為日期時間格式的字串。

--獲取日期:

values date(current timestamp);--2016/1/6

values current date;--2016/1/6

--獲取時間

values time(current timestamp);--16:40:40

values current time;--16:40:40

--獲取時間戳

values timestamp(current timestamp);--2016/1/6 16:40:40

values current timestamp;--2016/1/6 16:40:40

values sysdate;--2016/1/6 16:40:40

--獲取當前年份

values year(current timestamp);--2016

--獲取當前月

values month(current timestamp);--1

--獲取當前日

values day(current timestamp);--6

--獲取當前時

values hour(current timestamp);--16

--獲取分鐘

values minute(current timestamp);--40

--獲取秒

values second(current timestamp);--40

--獲取毫秒

values microsecond(current timestamp); --415269

2、db2時間可以直接加減帶單位的時間長度。

values timestamp('2016-1-06 12:18:12')+1 year;--2017/1/6 12:18:12

values timestamp('2016-1-06 12:18:12')+1 month;--2016/2/6 12:18:12

values timestamp('2016-1-06 12:18:12')+1 day;--2016/1/7 12:18:12

values timestamp('2016-1-06 12:18:12')+1 hour;--2016/1/6 13:18:12

values timestamp('2016-1-06 12:18:12')+1 minute;--2016/1/6 12:19:12

values timestamp('2016-1-06 12:18:12')+1 second;--2016/1/6 12:18:13

values timestamp('2016-1-06 12:18:12')+1 year+1 month+1 day+1 hour+1 minute +1 second ;--2017/2/7 13:19:13

values timestamp('2016-1-06 12:18:12')-1 year-1 month-1 day-1 hour-1 minute -1 second ;--2014/12/5 11:17:11

3、通過days()可以獲取兩個時間相差天數。

values days(current date)-days(current date-1000 day);--1000

4、通函式可以獲取時間對應的周(月)。

--dayname()返回指定日期的星期名,該星期名是由首字元大寫、其他字元小寫組成的英文名。

values dayname(current timestamp);--wednesday(當天為星期五)

--dayofweek()返回引數中的星期幾,用範圍在 1-7 的整數值表示,其中 1 代表星期日。

values dayofweek(current timestamp);--4(當天為星期三)

--dayofweek_iso()返回引數中的星期幾,用範圍在 1-7 的整數值表示,其中 1 代表星期一。

values dayofweek_iso(current timestamp);--3(當前為星期三)

--dayofyear()返回引數中一年中的第幾天,用範圍在 1-366 的整數值表示。

values dayofyear(current timestamp);--6

--monthname()對於引數的月部分的月份,返回乙個大小寫混合的字串(例如,january)。

values monthname(current timestamp);--january(當前為一月)

--week()返回引數中一年的第幾周,用範圍在 1-54 的整數值表示。以星期日作為一周的開始。(引數可以為日期格式或者日期格式的字串)

values week('2016-01-02');--1

values week('2016-01-03');--2

--week_iso()返回引數中一年的第幾周,用範圍在 1-54 的整數值表示。以星期一作為一周的開始。(引數可以為日期格式或者日期格式的字串)

values week_iso('2016-01-02');--53

values week_iso('2016-01-03');--53

values week_iso('2016-01-04');--1

5、timestampdiff(n,char(timestamp('2012-05-25 10:23:24')-timestamp('2012-05-25 10:20:24')))根據兩個時間戳記之間的時差,返回由第乙個引數定義的型別表示的估計時差。(整除部分,不會四捨五入)

n=2:秒 ; n=4 :分; n=8:時;

n=16 :天; n=32 :周;n=64:月;

n=128 :季度; n=256:年;

values timestampdiff(2,char(timestamp('2016-01-06 12:23:24')-timestamp('2016-01-06 12:20:24'))); --180秒

values timestampdiff(4,char(timestamp('2016-01-06 12:23:24')-timestamp('2016-01-06 10:20:22'))); --123分鐘

values timestampdiff(8,char(timestamp('2016-01-06 12:23:24')-timestamp('2016-01-06 10:20:22'))); --2小時

values timestampdiff(16,char(timestamp('2016-01-07 12:23:24')-timestamp('2016-01-06 15:20:22'))); --0天

values timestampdiff(16,char(timestamp('2016-01-07 12:23:24')-timestamp('2016-01-06 10:20:22'))); --1天

values timestampdiff(32,char(timestamp('2016-03-06 12:23:24')-timestamp('2016-01-06 10:20:22'))); --8周

values timestampdiff(64,char(timestamp('2016-03-06 12:23:24')-timestamp('2016-01-06 10:20:22'))); --2月

values timestampdiff(128,char(timestamp('2016-05-06 10:20:22')-timestamp('2016-02-06 10:20:22'))); --1季度

values timestampdiff(256,char(timestamp('2017-01-06 10:20:22')-timestamp('2016-01-06 10:20:22'))); --1年

DB2 日期時間函式

select current date from sysibm.sysdummy1 select current time from sysibm.sysdummy1 select current timestamp from sysibm.sysdummy1 sysibm.sysdummy1 表是...

DB2 日期時間函式

select current date from sysibm.sysdummy1 select current time from sysibm.sysdummy1 select current timestamp from sysibm.sysdummy1 sysibm.sysdummy1 表是...

db2與mysql編目 DB2 資料庫編目

方法一 命令執行方法 db2 catalog tcpip node node name remote server ip server server port l node name 所要編目的節點名,任意起。節點名在一台機器上必須是唯一的,不能跟已有的節點名重複。可通過db2 list node ...