DB2时间函数是我们最常见的函数之一,下面就为您介绍一些DB2时间函数,供您参考,希望可以让您对DB2时间函数有更多的了解。
--获取当前日期: selectcurrentdatefromsysibm.sysdummy1; valuescurrentdate; --获取当前日期 selectcurrenttimefromsysibm.sysdummy1; valuescurrenttime; --获取当前时间戳 selectcurrenttimestampfromsysibm.sysdummy1; valuescurrenttimestamp; --要使当前时间或当前时间戳记调整到GMT/CUT,则把当前的时间或时间戳记减去当前时区寄存器: valuescurrenttime-currenttimezone; valuescurrenttimestamp-currenttimezone; --获取当前年份 valuesyear(currenttimestamp); --获取当前月 valuesmonth(currenttimestamp); --获取当前日 valuesday(currenttimestamp); --获取当前时 valueshour(currenttimestamp); --获取分钟 valuesminute(currenttimestamp); --获取秒 valuessecond(currenttimestamp); --获取毫秒 valuesmicrosecond(currenttimestamp); --从时间戳记单独抽取出日期和时间 valuesdate(currenttimestamp); valuesVARCHAR_FORMAT(currentTIMESTAMP,'yyyy-mm-dd'); valueschar(currentdate); valuestime(currenttimestamp); --执行日期和时间的计算 valuescurrentdate+1year; valuescurrentdate+3years+2months+15days; valuescurrenttime+5hours-3minutes+10seconds; --计算两个日期之间的天数 valuesdays(currentdate)-days(date('2010-02-20')); --时间和日期换成字符串 valueschar(currentdate); valueschar(currenttime); --要将字符串转换成日期或时间值 valuestimestamp('2010-03-09-22.43.00.000000'); valuestimestamp('2010-03-0922:44:36'); valuesdate('2010-03-09'); valuesdate('03/09/2010'); valuestime('22:45:27'); valuestime('22.45.27'); --计算两个时间戳记之间的时差: --秒的小数部分为单位 valuestimestampdiff(1,char(currenttimestamp-timestamp('2010-01-01-00.00.00'))); --秒为单位 valuestimestampdiff(2,char(currenttimestamp-timestamp('2010-01-01-00.00.00'))); --分为单位 valuestimestampdiff(4,char(currenttimestamp-timestamp('2010-01-01-00.00.00'))); --小时为单位 valuestimestampdiff(8,char(currenttimestamp-timestamp('2010-01-01-00.00.00'))); --天为单位 valuestimestampdiff(16,char(currenttimestamp-timestamp('2010-01-01-00.00.00'))); --周为单位 valuestimestampdiff(32,char(currenttimestamp-timestamp('2010-01-01-00.00.00'))); --月为单位 valuestimestampdiff(64,char(currenttimestamp-timestamp('2010-01-01-00.00.00'))); --季度为单位 valuestimestampdiff(128,char(currenttimestamp-timestamp('2010-01-01-00.00.00'))); --年为单位 valuestimestampdiff(256,char(currenttimestamp-timestamp('2010-01-01-00.00.00')));
【编辑推荐】
DB2公共表表达式的用法
跨数据库访问DB2表的方法
DB2表空间大小的限制
深入解读DB2存储过程
DB2连接服务器的配置