oracle获取日期差方法

  • 方法一 获取带小数的样式
select to_date('2020-01-05 00:00:00','yyyy-mm-dd hh24:mi:ss')-to_date('2020-01-03 11:05:00','yyyy-mm-dd hh24:mi:ss') dds from dual

结果1
在这里插入图片描述

  • 方法二 获取整日期差
select (trunc(to_date('2020-01-05 00:00:00','yyyy-mm-dd hh24:mi:ss')) - trunc(to_date('2020-01-03 11:05:00','yyyy-mm-dd hh24:mi:ss'))) dd from dual

结果2
在这里插入图片描述

  • 实际使用场景
Select count(*) 总人数,
sum(trunc(in_hospital_time) - trunc(out_hospital_time)+1 ) 住院天数
From Users  
Where  out_hospital_time Between to_date('2020-03-01 00:00:00','yyyy-mm-dd hh24:mi:ss') And to_date('2020-03-31 23:59:59','yyyy-mm-dd hh24:mi:ss') 

结果3
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Qcg0223/article/details/105655208