oracle 计算天,Oracle 计算两个日期相差天时分秒

1、用到oracle相关函数

round、trunc、to_number、to_date

1.1 round(number[,decimals]) 四舍五入

number:指需要处理的数值,是必须填写的值。

decimals:指在进行四舍五入运算时 , 小数的应取的位数,该参数可以不填,不填的时候,系统默认小数位数取0。

1.2 trunc(number[,num_digits])

number:指需要处理的数值,是必须填写的值。

num_digits:用于指定取整精度的数字,截取时不进行四舍五入。该参数可以不填,不填的时候num_digits 的默认值为 0。

expr:如果expr为char、nchar、varchar2,nvarchar2则可以指定格式fmt,如果是binary_float、binary_double则不能指定格式

nlsparam: 用于数字转换的TO_CHAR函数中的nlsparam参数具有相同的目的。有关更多信息,请参阅TO_CHAR(nuber)。此功能不直接支持CLOB数据。然而,CLOB可以通过隐式数据转换作为参数传入

char:需要转换的字符

fmt:是一种指定char格式的日期时间模型格式。如果省略FMT,则char必须是默认日期格式。默认日期格式由NLS_Area初始化参数隐式确定,也可以由NLS_DATE_FORCT参数显式设置。如果fmt是J,对于Julian,char必须是一个整数。

nlsparam: 指定正在转换为日期的文本字符串的语言。该参数可以具有以下形式

1.5 mod(n1,n2) 取余(n1/n2的小数部分)

n1,n2 都为数字类型

相差时分秒

第一种

e6a1a46d3a9b7837774a3b17fc24a554ff7.jpg

052bfcf193ce14e1c8df922ebd37f900f5a.jpg

--4天2小时10分 =353405秒

select trunc(353405/(24*60*60)) 天,trunc( mod(353405,24*60*60)/(60*60) ) 时,

trunc(mod(353405,60*60)/60) 分,trunc(mod(353405,60)) 秒 from dual;

View Code

27e28cf889d4a1eb4ad6abd601d98ae7.png

第二种

两个日期相减得出一个数字类型

1天2小时5分10秒=1.0869212962963

select to_date('2020-01-06 14:35:35','yyyy-mm-dd hh24:mi:ss')-

to_date('2020-01-05 12:30:25','yyyy-mm-dd hh24:mi:ss') from dual;

select trunc(1.0869212962963) 天 ,

trunc(mod(1.0869212962963, 1) * 24) 时,

trunc(mod(mod(1.0869212962963, 1) * 24, 1) * 60) 分,

trunc(mod(mod(mod(1.0869212962963, 1) * 24, 1) * 60,1)*60) 秒

from dual;

ebf79f79694e0f04fe7631754723f81b.png

第三种

5774b30e107ce5bf74b8aad9ad391c7c3bd.jpg

33106a464551910b6975474d7ada28e0490.jpg

select startTime, endTime, tday, thour, tminute, round((tt_minute-tminute)*60) astsecondsfrom(select startTime, endTime, tday, thour, trunc((tt_hour-thour)*60) as tminute, (tt_hour-thour)*60 astt_minutefrom(select startTime, endTime, tday, trunc((tt_day-tday)*24) as thour, (tt_day-tday)*24 as tt_hour from(select to_number(endTime - startTime) astt_day,

trunc(to_number(endTime- startTime)) as tday, startTime, endTime from(select to_date('2012-11-01 00:20:31','yyyy-mm-dd hh24:mi:ss') as startTime,sysdate as endTime fromdual)

)

)

)

View Code

f912e4d2ef30197dd68197986a25a710.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值