DB

두 날짜 시간의 차 구하기..

달려라곰간지 2013. 7. 30. 16:31

//두시간의 차이를 초로 가져오기.

select

      (trunc(MOD(to_date(a.resusteamcalldd || a.resusteamcalltm, 'yyyymmddhh24miss')
     - to_date(a.cardarrestmdd || a.cardarrestmtm, 'yyyymmddhh24miss'), 1) *24 *60*60))

from a

//위와같이 했을 경우에는 두시간의 차이.. 10분 차이가 나면 600이란 값이 나온다..

 

시간이나 분단위를 구하기 위해서는..date형식으로 바꾼후 char형식으로 변환해서 format설정을 하면된다..

select

to_char(to_date(trunc(MOD(to_date(a.resusteamcalldd || a.resusteamcalltm, 'yyyymmddhh24miss')
     - to_date(a.cardarrestmdd || a.cardarrestmtm, 'yyyymmddhh24miss'), 1) *24 *60*60), 'sssss'), 'hh24mi')

from a

--위의 포맷은 0010(10분)  0110(1시간 10분) 이런식으로 가져온다..

-- 단, to_date함수에서는 -값이 들어올수 없으므로, 그런경우에는 따로 예외처리를 해주어야한다..