게시일: Oct 17, 2011 2:55:51 AM
- SYSDATE : 시스템의 현재 날짜/시간
- CURRENT_DATE : 세션별 시간대(TIME_ZONE)를 기준으로 한 날짜/현재 시간. Oracle은 세션별로 시간대 설정 가능 (파라미터 : TIME_ZONE)
- SYSTIMESTAMP : 시스템의 현재 날짜/시간 (반환 타입이 DATE가 아닌 TIMESTAMP WITH TIME ZONE 타입을 반환)
- CURRENT_TIMESTAMP : 세션별 시간대(TIME_ZONE)를 기준으로 한 날짜/현재 시간 (반환 타입이 DATE가 아닌 TIMESTAMP WITH TIME ZONE 타입을 반환)
파라미터 있음(TIMESTAMP WITH TIME ZONE의 정밀도. 생략시는 default 6)
- LOCALTIMESTAMP : (반환 타입이 TIMESTAMP WITH TIME ZONE 아닌 TIMESTAMP 타입을 반환)
파라미터 있음(TIMESTAMP WITH TIME ZONE의 정밀도. 생략시는 default 6)
select sysdate --> 2011-10-17 10:52:36
, current_date --> 2011-10-17 10:52:36
, systimestamp --> 17-OCT-11 10.52.36.043602 AM +09:00
, current_timestamp --> 17-OCT-11 10.52.36.043602 AM +09:00
, current_timestamp(6) --> 17-OCT-11 10.52.36.043602 AM +09:00
, current_timestamp(5) --> 17-OCT-11 10.52.36.04360 AM +09:00
, current_timestamp(4) --> 17-OCT-11 10.52.36.0436 AM +09:00
, current_timestamp(3) --> 17-OCT-11 10.52.36.044 AM +09:00
, current_timestamp(2) --> 17-OCT-11 10.52.36.04 AM +09:00
, current_timestamp(1) --> 17-OCT-11 10.52.36.0 AM +09:00
, current_timestamp(0) --> 17-OCT-11 10.52.36 AM +09:00
, current_timestamp(-1) --> ORA-30088: datetime/interval precision is out of range
, localtimestamp --> 17-OCT-11 10.54.36.043602 AM
, localtimestamp(6) --> 17-OCT-11 10.54.36.043602 AM
, localtimestamp(5) --> 17-OCT-11 10.54.36.04360 AM
, localtimestamp(4) --> 17-OCT-11 10.54.36.0436 AM
, localtimestamp(3) --> 17-OCT-11 10.54.36.044 AM
, localtimestamp(2) --> 17-OCT-11 10.54.36.04 AM
, localtimestamp(1) --> 17-OCT-11 10.54.36.0 AM
, localtimestamp(0) --> 17-OCT-11 10.54.36 AM
, localtimestamp(-1) --> ORA-30088: datetime/interval precision is out of range
from dual;
--> sysdate 외에는 아직 써 본적이 없네요.
*** 실수로 데이터를 변경했을 때 좋은 것 같아요 ***
1. 특정시간의 데이터 조회
select * from 테이블명 as of timestamp to_date('2011101612','yyyymmddhh24') where rownum < 10;
2. 한시간 전 데이터 조회
select * from 테이블명 as of timestamp (systimestamp - interval '1' hour) where rownum < 10;
3. 1분전 데이터 조회
select * from 테이블명 as of timestamp (systimestamp - interval '1' minute) where rownum < 10;
- ADD_MONTHS(date, integer) : 임의의 날짜에 개월 수를 더함
select trunc(sysdate), add_months(trunc(sysdate), 2) from dual;
-----------------------------------------------------------------------
2011-10-17 00:00:00 2011-12-17 00:00:00
- MONTHS_BETWEEN(date1, date2) : 두 날짜 사이의 개월 수
select months_between(to_date('20111207', 'yyyymmdd'), to_date('20110607', 'yyyymmdd')) from dual;
---------------------------------------------------------------------------------------------------
6
- NEXT_DAY(date, char) : date이후의 날짜 중에서 주중에 char로 명시된 첫 번째 일자를 반환
char : MONDAY, MON(SUN, MON, TUE, WED, THUR, FRI, SAT), 2 (1:일요일, 7:토요일)
select next_day(trunc(sysdate), 'TUESDAY'), next_day(trunc(sysdate), 'TUE'), next_day(trunc(sysdate), 3) from dual;
-------------------------------------------------------------------------------------------------------------------
2011-10-18 00:00:00 2011-10-18 00:00:00 2011-10-18 00:00:00
- ROUND(date, fmt) : 파라미터로 들어오는 date 날짜를 포맷모델인 fmt에 의해 명시된 단위로 반올림한 결과를 반환
select round(trunc(sysdate), 'cc' ) century --> 2001-01-01 00:00:00
, round(trunc(sysdate), 'yyyy' ) next_year1 --> 2012-01-01 00:00:00
, round(trunc(sysdate), 'year' ) next_year1 --> 2012-01-01 00:00:00
, round(trunc(sysdate), 'iyyy' ) iso_year --> 2012-01-02 00:00:00 .. iso 년도 기준
, round(trunc(sysdate), 'q' ) quarter --> 2011-10-01 00:00:00
, round(trunc(sysdate), 'month') mon --> 2011-11-01 00:00:00
, round(trunc(sysdate), 'dd' ) dd --> 2011-10-17 00:00:00
, round(trunc(sysdate), 'day' ) day --> 2011-10-16 00:00:00
from dual;
-----------------------------------------------------------------
포맷 모델 단위
-----------------------------------------------------------------
CC 4자리연도의 끝 두 자리를 기준으로 반올림된다.
SCC
-----------------------------------------------------------------
SYYYY 년(7월 1일부터 반올림된다)
YYYY
YEAR
SYEAR
YYYY
YYY
Y
-----------------------------------------------------------------
IYYY ISO 기준 년(year)
IYY
IY
I
-----------------------------------------------------------------
Q 분기(한 분기의 두 번째 달의 16일부터 반올림된다)
-----------------------------------------------------------------
MONTH 월(16일부터 반올림된다)
MON
MM
RM
-----------------------------------------------------------------
WW 연도의 첫 번째 날짜로 그 주의 같은 날
-----------------------------------------------------------------
IW ISO 연도의 첫 번째 날짜로그 주의 같은 날
-----------------------------------------------------------------
WW 월의 첫 번째 날짜로 그 주의 같은 날
-----------------------------------------------------------------
DDD, DD, J 일
-----------------------------------------------------------------
DAY, DY, D 한 주가 시작되는 날짜
-----------------------------------------------------------------
HH, HH12, HH24 시
-----------------------------------------------------------------
MI 분
-----------------------------------------------------------------
- TRUNC(date, fmt) : date 날짜를 포맷모델에 맞게 날짜를 잘라낸다.
round 함수는 포맷모델에 따라 일정한 날짜를 기준으로 해서 반올림이 수행되는데 반해
trunc 함수는 이에 상관없이 무조건 잘라낸다.
select trunc(sysdate ) today --> 2012-02-16 00:00:00
, trunc(sysdate, 'cc' ) century --> 2001-01-01 00:00:00
, trunc(sysdate, 'yyyy' ) year1 --> 2012-01-01 00:00:00
, trunc(sysdate, 'year' ) year2 --> 2012-01-01 00:00:00
, trunc(sysdate, 'iyyy' ) iso_year --> 2012-01-02 00:00:00 .. iso 년도 기준
, trunc(sysdate, 'q' ) quarter --> 2012-01-01 00:00:00
, trunc(sysdate, 'month') mon --> 2012-02-01 00:00:00
, trunc(sysdate, 'dd' ) dd --> 2012-02-16 00:00:00
, trunc(sysdate, 'day' ) day --> 2012-02-12 00:00:00
from dual;
select sysdate --> 2015-11-10 18:30:55
, trunc(sysdate, 'mi') --> 2015-11-10 18:30:00
, trunc(sysdate, 'hh24') --> 2015-11-10 18:00:00
, trunc(sysdate) --> 2015-11-10 00:00:00
from dual
;
--> round와 trunc 함수 모두 포맷 모델을 생략할 수 있는데 이러한 경우 파라미터 값에 가장 가까운 날짜로 반올림되거나 잘린다.
- EXTRACT([YEAR] [MONTH] [DAY] [HOUR] [MINUTE] [SECOND]) ... FROM datetime)
: 파라미터로 들어오는 날짜정보에서 특정한 날짜 유형, 즉 연도나 월, 시간, 분, 초 등을 추출하여 그 결과를 반환
select extract(year from sysdate ) year --> 2011
, extract(month from sysdate ) month --> 10
, extract(day from sysdate ) day --> 17
, extract(hour from systimestamp) hour --> 4
, extract(minute from systimestamp) minute --> 34
, extract(second from systimestamp) second --> 21.423162
, extract(year from systimestamp) --> 2011
, extract(hour from sysdate ) --> ORA-30076: invalid extract field for extract source
from dual;
--> M.Jay 의견 : 기존에는 이런 데이터를 구할 때 trunc를 하거나 substr등을 사용하고는 했는데 이 함수를 사용하면 좋겠네요.
- 기타 날짜형 함수
ㅇDBTIMEZONE : 데이터베이스의 시간대 반환
select DBTIMEZONE from dual;
----------------------------
-04:00
ㅇSESSIONTIMEZONE : 현재 접속되어 있는 세션의 시간대 반환
select SESSIONTIMEZONE from dual;
---------------------------------
+09:00