[    ORACLE 날짜함수, 변환함수, decode(), case()  ]  





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
--[ ** 날짜 함수 ** ]
 
--sysdate : 현재 시스템 날짜를 구해옴
select sysdate AS 현재날짜 from dual;
 
--두 날짜 사이의 개월수를 출력하는 함수  : months_between(date1,date2) 
select ENAME,months_between(sysdate,hiredate) as 근무개월수 from EMP;
 
--개월수를 더하는 메서드 : add_months()
select add_months(sysdate,4) from dual; -- 현재 날짜에서 4개월을 더해줌
 
--다가올 특정일 날짜 가져오기 : next_day
select next_day(sysdate,'일요일') from dual; -- 현재날짜 기준 다가올 일요일의 날짜
 
--해당달의 마지막 일 수 구하기 : last_day()
select LAST_DAY(SYSDATE) from dual;
 
--형식을 지정해 날짜를 문자열로 변환시킨다. : to_char()
select to_char(sysdate,'yyyy-MM-dd') from dual;
 
--문자열을 날짜형으로 바까주는 메서드 : to_DATE()
select to_DATE('2017/10/27','yyyy/mm/dd') from dual;
 
--NULL일 경우 다른 값으로 치환하는 메서드(중요!! NULL과 산술연산시 결과는 무조건 NULL이기 때문에 적당한 처리 필요)   : NVL
select (NVL(sal,0) * 0.09) 세금 from EMP; -- sal값이 NULL인경우 0으로 계산되도록...
 
--switch문과 같은 역할을 하는 sql 메서드 : decode()
    -- deptno가 20일경우 '리서치부서'로 30일경우 '영업부'로 40일경우 '운영부'로 출력하는 코드
select DEPTNO, decode(deptno,20,'리서치부서',30,'영업부',40,'운영부') from DEPT;
 
--elseif문과 유사 : case()
select ENAME,DEPTNO,
  case when DEPTNO = 20 then '리서치부'
       when DEPTNO = 30 then '영업부'
       when DEPTNO = 40 then '운영부'
       else ' '
       end AS "부서명"
from EMP;


+ Recent posts