Oracle 기본정리 - 날짜,문자 함수4(sysdate, to_char, to_date, to_number, add_months, next_day)
Oracle 기본정리 - 날짜,문자 함수4(sysdate, to_char, to_date, to_number, add_months, next_day)
기본형태
숫자 : to_number
문자 : to_char
날짜 : to_date
변환형태
--- to_char ---> ---- to_date --->
숫자 문자 날짜
<---to_number---- <--- to_char ---
--sysdate 날짜출력
select sysdate from dual;
--sysdate 날짜 시간 출력
select to_char(sysdate,'YYYY-MM-DD HH:MI:SS') from dual;
--sysdate 사칙연산
select sysdate-1 as "어제" , sysdate as "오늘", sysdate+1 as "내일" from dual;
select to_char(sysdate,'yyyy-mm-dd hh:mi:dd'), to_char(sysdate+3/24,'yyyy-mm-dd hh:mi:dd') from dual; --sysdate 1/8 덧셈
--add_months(날짜,숫자) 날짜기준 + (숫자)달 후의 날짜
select sysdate, add_months(sysdate,3) from dual;
--last_day(날짜) 해당 날짜의 마지막 일
select last_day(sysdate) from dual;
select last_day(to_date('2010-01-01','yyyy-mm-dd')) from dual;
select last_day(to_date('2016-02-01','yyyy-mm-dd')) from dual;
--next_day next_day(날짜,'요일') 돌아오는 요일 검색, 날짜 출력
select next_day(sysdate,'화') from dual;
select next_day(sysdate,'금요') from dual;
select next_day(sysdate,'금요일') from dual;
select round(sysdate) from dual;
--round(날짜,'삽입문자') 삽입문자(yyyy,mm,dd) 기준 반올림
select round(to_date('2019-11-16'),'mm') from dual;
select round(to_date('2019-11-16'),'year') from dual;
select round(sysdate,'y') from dual;
select round('2019-11-16','mm') from dual; --실행안됨
--trunc(날짜,'삽입문자') 삽입문자 기준으로 자름
select to_char(trunc(sysdate,'yyyy'),'yyyy-mm-dd hh:mi:ss') from dual;
select to_char(trunc(sysdate,'mm'), 'yyyy-mm-dd hh:mi:ss') from dual;
select to_char(trunc(sysdate,'dd'),'yyyy-mm-dd hh:mi:ss') from dual;
--months_between(시작날짜,마지막날짜) 시작과 마지막 날사이 일 수 구함, 차이 구함
select months_between(sysdate, '2019-01-01') from dual;
--예제 to_date
select sysdate - '2019-01-01' from dual; --안됨, 문자라고 인식함
select sysdate - to_date('2019-01-01') from dual; --잘됨
--예제2 to_number
select 3000-'2,000' from dual; --안됨, 문자라고 인식함
select 3000-to_number('2,000','9,999') from dual; --잘됨
--예제3 to_char
select to_char(sysdate,'yyyy') from dual;
select to_char(sysdate,'yyyy-mm') from dual;
--예제4 to_char
select to_char(10000,'9,999,999') from dual; --출력 : 10,000
select to_char(10000,'L9,999,999') from dual; --출력 : ₩10,000
select to_char(10000,'$9,999,999') from dual; --출력 : $10,000
--예제5 to_char
select to_char(1200000,'L9,999,999') from dual; --출력 : ₩1,200,000
select to_char(1200000,'L9,999') from dual; --출력 : ######### (포멧보다 넘어서면 오류)
select sysdate-hiredate from emp; --일한 일수
select (sysdate-hiredate)/365 from emp; --일한 년수
--모든 날짜 출력
select to_char(sysdate,'yyyy yy mm mon month dd ddd d day') from dual;
--24시간 12시간
select to_char(sysdate,'hh hh24 am hh') from dual;
--문제 82년도에 입사한 사람 출력
select * from emp where substr(hiredate,1,2)='82'; --방법1
select * from emp where to_char(hiredate,'yyyy')='1982'; --방법2
select * from emp where trunc(hiredate, 'yyyy')='1982-01-01'; --방법4
'프로그래밍 정리 > Oracle' 카테고리의 다른 글
Oracle 기본정리 - 수학 함수6(sum, avg, count) (0) | 2019.11.29 |
---|---|
Oracle 기본정리 - 함수5(decode, case when) (0) | 2019.11.29 |
Oracle 기본정리 - 문자함수3(substr, trim, pad, instr, like) (0) | 2019.11.29 |
Oracle 기본정리 - 문자함수2(concat, inicap,lower,upper, length,lengthB, substr) (0) | 2019.11.29 |
Oracle 기본정리 - 기본함수1(abs,ceil,floor,exp,log,mod,power,round,trunc) (0) | 2019.11.29 |