oracle獲取年月日,兩個日期相減

2022-05-09 08:06:10 字數 2026 閱讀 7127

oracle如何計算兩個日期的相差天數、月數、年數、小時數、分鐘數、秒數

1、相差天數(兩個日期相減)

--oracle中兩個日期相差天數--

select to_number(to_date('2018-6-5','yyyy-mm-dd hh24:mi:ss')- to_date('2018-5-31','yyyy-mm-dd hh24:mi:ss'))

as 相差天數 from dual;

2、相差小時數、分鐘數、秒數

--oracle中兩個日期相差小時數--

select to_number((to_date('2018-6-5','yyyy-mm-dd hh24:mi:ss')- to_date('2018-5-31','yyyy-mm-dd hh24:mi:ss'))*24)

as 相差小時數 from dual;

--oracle中兩個日期相差分鐘數--

select to_number((to_date('2018-6-5','yyyy-mm-dd hh24:mi:ss')- to_date('2018-5-31','yyyy-mm-dd hh24:mi:ss'))*24*60)

as 相差分鐘數 from dual;

--oracle中兩個日期相差秒數--

select to_number((to_date('2018-6-5','yyyy-mm-dd hh24:mi:ss')- to_date('2018-5-31','yyyy-mm-dd hh24:mi:ss'))*24*60*60)

as 相差秒數 from dual;

3、相差月數(months_between()函式)

--oracle兩個日期的相差月數--

--1)月份都是最後一天,a日期 > b日期 ,返回整數 ---

select months_between(to_date('2018-6-30','yyyy-mm-dd hh24:mi:ss'),to_date('2018-5-31','yyyy-mm-dd hh24:mi:ss'))

as 相差月份1 from dual;

--2)月份都是最後一天,b日期 > a日期 ,返回負數 ---

select months_between(to_date('2018-4-30','yyyy-mm-dd hh24:mi:ss'),to_date('2018-5-31','yyyy-mm-dd hh24:mi:ss'))

as 相差月份2 from dual;

--3)月份天數不一樣,a日期 > b日期 ,返回帶小數的數字---

select months_between(to_date('2018-6-25','yyyy-mm-dd hh24:mi:ss'),to_date('2018-5-31','yyyy-mm-dd hh24:mi:ss'))

as 相差月份3 from dual;

4、相差年數(原先月數/12)

--oracle兩個日期的相差年份--

select ((months_between(to_date('2018-5-31','yyyy-mm-dd hh24:mi:ss'),to_date('2016-5-31','yyyy-mm-dd hh24:mi:ss')))/12)

as 相差年份 from dual;

顯示結果如下:

1)相差天數

2)相差小時數、分鐘數、毫秒數

3)相差月數

4)相差年數

oracle獲取年月日,兩個日期相減

需求 查詢剩餘多少天?先看一下我的實際案例 select ceil t.mbdcsj sysdate as remainingdays from t zg mb mbglb t 測試案例 獲取兩時間的相差小時數 select ceil to date 2019 05 02 00 00 00 yyyy...

計算兩個日期相差的年,月,日

計算兩個日期相差的年和月 param fromdate 開始時間 param todate 結束時間 return 想相差年月拼到一起,用逗號隔開,前面為相差年 public static stringbuffer calculator string fromdate,string todate 測...

MySQL計算兩個日期相差的年月日

datediff 結束時間,開始時間 select datediff 2020 06 13 2020 06 12 as diffdate下面這個加了個日期格式化 select datediff 2020 06 13 date format now y m d as diffdatetimestamp...