db2时间运算

    技术2025-06-12  23

    对DB2中的时间常规处理:1、to_number(time(replace(pr.prclosetime||':00','/','-'))-time(replace(pr.propentime||':00','/','-')))/10000 由于我的数据库中时间是以yyyy/MM/dd hh:mm形式的字符串方式存储的,所以这里用replace将/换成-,/转时间时是不认的,':00'是将yyyy/MM/dd hh:mm补全成yyyy/MM/dd hh:mm:ss,否则时间转换不过去,to_number将结果转成数值型,以便做其它运算,但是转出来的时间是正常时间去掉分隔符后的字串,运算时需要另行处理。

    2、int(days(replace(pr.prclosetime||':00','/','-'))-days(replace(pr.propentime||':00','/','-')))*24+case when int(time(replace(pr.prclosetime||':00','/','-'))-time(replace(pr.propentime||':00','/','-')))/10000>=1 then  to_number(time(replace(pr.prclosetime||':00','/','-'))-time(replace(pr.propentime||':00','/','-')))/10000 else 0 end  as stimeint是将结果转成整型,days是取成日,time是取的具体时间,case when then 中便是处理了

    3、to_date(data_times,'yyyy/mm/dd hh24:mi') as data_times将字符串按指定格式转成时间

    还有很多其它形式的时间操作,这里就不多说了,随时备查吧~~

    最新回复(0)