oracle 日期相減

2022-05-15 12:38:47 字數 1691 閱讀 2062

oracle日期相減

2012-02-10 12:18

--months_between(date2,date1) 

給出date2-date1的月份 

sql> select months_between('19-12月-1999','19-3月-1999') mon_between from dual; 

mon_between 

----------- 

9 sql>select months_between(to_date('2000.05.20','yyyy.mm.dd'),to_date('2005.05.20','yyyy.dd')) mon_betw from dual; 

mon_betw 

--------- 

-60 

oracle計算時間差表示式 

--獲取兩時間的相差豪秒數 

select ceil((to_date('2008-05-02 00:00:00' , 'yyyy-mm-dd hh24-mi-ss') - to_date('2008-04-30 23:59:59' , 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60 * 60 * 1000) 相差豪秒數 from dual; 

/* 相差豪秒數 

---------- 

86401000 

1 row selected 

*/ --獲取兩時間的相差秒數 

select ceil((to_date('2008-05-02 00:00:00' , 'yyyy-mm-dd hh24-mi-ss') - to_date('2008-04-30 23:59:59' , 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60 * 60) 相差秒數 from dual; 

/* 相差秒數 

---------- 

86401 

1 row selected 

*/ --獲取兩時間的相差分鐘數 

date型

select ceil(((to_date('2008-05-02 00:00:00' , 'yyyy-mm-dd hh24-mi-ss') - to_date('2008-04-30 23:59:59' , 'yyyy-mm-dd hh24-mi-ss'))) * 24 * 60) 相差分鐘數 from dual; 

timestamp(6)型

select t.calorie_consuming calorieconsuming ,t.write_date writedate ,t.start_time start_time ,t.end_time end_time

,ceil(((to_date(to_char(t.end_time ,'yyyy-mm-dd hh24-mi-ss') , 'yyyy-mm-dd hh24-mi-ss') - to_date(to_char(t.start_time ,'yyyy-mm-dd hh24-mi-ss') , 'yyyy-mm-dd hh24-mi-ss'))) * 24 * 60) duration

from t_sports t where 1=1 and t.is_delete=0 and t.user_id=?

order by t.write_date desc,t.write_time asc) tsp group by tsp.writedate order by tsp.writedate desc

ORACLE中的日期相減

最近做醫院的系統,有很多資料要求求出乙個時間段來,比如手術執行多長時間,患者住了多長時間的院等。每次都是baidu,後來發現在oracle中,兩個日期相減的差是以天計算的。如 select to date 2012 10 02 12 30 00 yyyy mm dd hh mi ss to date...

Oracle與Mysql的日期相減

一 oracle日期相減 1 兩個date型別資料相差的小時數 2 兩個timestamp型別資料相差的小時數 3 乙個date型別資料和乙個timestamp型別資料相差的小時數 兩個date型別資料的相差的小時數 select t.end time,t.begin time,round time...

oracle中兩個日期相減

oracle兩個時間相減預設的是天數 oracle 兩個時間相減預設的是天數 24 為相差的小時數 oracle 兩個時間相減預設的是天數 24 60 為相差的分鐘數 oracle 兩個時間相減預設的是天數 24 60 60 為相差的秒數 months between date2,date1 給出d...