DB2中使用方式
計算兩個時間戳記之間的時差:
--秒的小數部分為單位
values timestampdiff(1,char(current timestamp - timestamp('2010-01-01-00.00.00')));
--秒為單位
values timestampdiff(2,char(current timestamp - timestamp('2010-01-01-00.00.00')));
--分為單位
values timestampdiff(4,char(current timestamp - timestamp('2010-01-01-00.00.00')));
--小時為單位
values timestampdiff(8,char(current timestamp - timestamp('2010-01-01-00.00.00')));
--天為單位
values timestampdiff(16,char(current timestamp - timestamp('2010-01-01-00.00.00')));
--周為單位
values timestampdiff(32,char(current timestamp - timestamp('2010-01-01-00.00.00')));
--月為單位
values timestampdiff(64,char(current timestamp - timestamp('2010-01-01-00.00.00')));
--季度為單位
values timestampdiff(128,char(current timestamp - timestamp('2010-01-01-00.00.00')));
--年為單位
values timestampdiff(256,char(current timestamp - timestamp('2010-01-01-00.00.00')));
在調用timestampdiff轉換的時候,兩個日期是必須是日期格式,如果是varchar需要用timestamp來轉換,調用timestampdiff轉換后的結果是char(22)的類型