Background Image
조회 수 6369 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
○ 테이블 스키마
create table  diff_datetime ( start_datetime  datetime,  end_datetime  datetime  ) ;

○ 테스트 데이타
insert into diff_datetime ( start_datetime,  end_datetime ) values (to_datetime('20160510053140','YYYYMMDDHH24MISS'), to_datetime('20160517165930', 'YYYYMMDDHH24MISS') ) ; 
insert into diff_datetime ( start_datetime,  end_datetime ) values (to_datetime('20160509000510','YYYYMMDDHH24MISS'), to_datetime('20160517090723', 'YYYYMMDDHH24MISS') ) ; 
insert into diff_datetime ( start_datetime,  end_datetime ) values (to_datetime('20160516011258','YYYYMMDDHH24MISS'), to_datetime('20160517203243', 'YYYYMMDDHH24MISS') ) ; 
insert into diff_datetime ( start_datetime,  end_datetime ) values (to_datetime('20160517011258','YYYYMMDDHH24MISS'), to_datetime('20160517203243', 'YYYYMMDDHH24MISS') ) ; 


○ SQL 
select 
      start_datetime
    , end_datetime
    ,  ( end_datetime - start_datetime ) / (1000 * 60 * 60* 24 )   || '일 ' 
     || TO_CHAR(  TO_DATETIME ('00010101000000', 'YYYYMMDDHH24MISS' )    +  ( end_datetime - start_datetime  ), 'HH24:MI:SS' ) AS diff_time   
from  diff_datetime 




○ SQL 실행결과
  start_datetime                 end_datetime                   diff_time
====================================================================================
  05:31:40.000 AM 05/10/2016     04:59:30.000 PM 05/17/2016     '7일 11:27:50'
  12:05:10.000 AM 05/09/2016     09:07:23.000 AM 05/17/2016     '8일 09:02:13'
  01:12:58.000 AM 05/16/2016     08:32:43.000 PM 05/17/2016     '1일 19:19:45'
  01:12:58.000 AM 05/17/2016     08:32:43.000 PM 05/17/2016     '0일 19:19:45'



○ 설명
일 계산은 end_datetime에서 start_datetime을 차감하면 밀리세컨드 결과가 계산 된다.
1일은 1000 밀리세컨드 * 60초 * 60분 * 24시간임으로   ( end_datetime - start_datetime )  / (1000 * 60 * 60* 24 )  으로 계산하면 된다.
참고로 예제 연산 처리순서가 나누기 연산이 더 빠르기 때문에 괄호를 사용하여 연산순서를 조정하였다.

시간 계산 부분은
TO_CHAR 함수의 날짜 포맷을 맞추기 위하여 TO_DATETIME ('00010101000000', 'YYYYMMDDHH24MISS' ) 를 사용하였다.
0001년 1월 1일에 end_datetime - start_datetime 결과를 더한 후에 시간을 추출하였다.

날짜 연산에 대한 출력을 "?일 ?시:?분:?초"로 출력 할 때는 유용한 방법이다.


List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
259 CUBRID 매니저 데이터베이스 자동시작 설정(CUBRID 7.3.0). 윤희서 2009.01.17 23155
258 CUBRID 매니저 데이터베이스 자동시작 설정(CUBRID 2008 R 1.2). 1 윤희서 2009.01.31 20187
257 CUBRID 매니저 CUBRID Manager Client 에러(Java관련). file 윤희서 2009.02.06 24152
256 질의작성 요일 값을 반환 받을 수 있는 질의 형태 정만영 2009.03.13 17108
255 질의작성 다중 row를 하나의 column에 넣기 정만영 2009.03.13 20089
254 CUBRID 매니저 여러 버전의 CUBRID에 CUBRID Manager로 연결하는 방법 일동차렷? 2009.03.14 15355
253 질의작성 컬럼 사이즈 변경 방법 정만영 2009.03.14 24338
252 질의작성 테이블 데이터 복사 하기 정만영 2009.03.14 22302
251 기타 2008.11.18. 최신 우편번호부 with CUBRID file Prototype 2009.03.17 14482
250 질의작성 데이터 값 작은 따옴표(') 넣기 정만영 2009.03.20 23813
249 CUBRID 매니저 UTF8 로 저장된 데이터를 매니저로 조회하는 방법 file Prototype 2009.03.21 25636
248 기타 32bit 리눅스 환경에서 php 모듈 빌드가 되지 않을 경우 Prototype 2009.03.31 17179
247 운영관리 CUBRID 2008 설치시 주의사항 file seongjoon 2009.04.01 17499
246 운영관리 CUBRID 2008 삭제시 주의사항 file seongjoon 2009.04.01 15288
245 운영관리 CSQL 에서 한글을 입력하는 방법 Prototype 2009.04.01 19350
244 운영관리 큐브리드매니져를 이용한 데이터베이스 자동 백업 남재우 2009.04.01 19052
243 운영관리 데이터베이스 볼륨 자동 증가 설정 남재우 2009.04.01 18808
242 응용개발 jdbc에서 bit 데이터 타입 사용하기 손승일 2009.04.11 15014
241 운영관리 CUBRID 2008 삭제시 제어판에서 삭제되지 않을시 해결법 file seongjoon 2009.04.13 15892
240 CUBRID 매니저 java.lang.OutOfMemoryError 처리 방법 file janus 2009.04.13 17880
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 14 Next
/ 14

Contact Cubrid

대표전화 070-4077-2110 / 기술문의 070-4077-2113 / 영업문의 070-4077-2112 / Email. contact_at_cubrid.com
Contact Sales