ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • oracle 날짜함수, 변환함수, decode, case
    DataBase/oracle 2020. 3. 15. 19:32

     

    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 from dual; -- 20/03/15
     
    -- months_between(data1, data2) 두개의 파라미터를 이용하여 두 날짜 사이의 개월수 계산
    select first_name, last_name, months_between(sysdate, hire_date) from employees where department_id = 50;
     
    -- add_months() 현재 날짜에서 7개월 더하기
    select add_months(sysdate, 7from dual; -- 20/10/15
     
    -- next_day() 오늘부터 가장 가까운 해당일
    select next_day(sysdate, '일요일'from dual; -- 20/03/22
     
    -- last_day() 해당 달의 마지막 일수
    select last_Day(sysdate) from dual; -- 20/03/31
     
    -- to_char() data타입을 char타입으로
    select to_char(sysdate, 'yyyy-mm-dd'from dual; -- 2020-03-15
     
    -- to_date() char타입을 data
    select to_Date('2020/03/04''yyyy/mm/dd'from dual; -- 2020-03-15
     
    -- nvl() null 값을 다른 데이터로 변경하는 함수
    select first_name, last_name, nvl(commission_pct, 0from employees order by commission_pct; -- commission_pct 가 null 인 경우 0으로 출력!
     
    --decode() switch문과 같은 역할을 함
    select department_id, decode(department_id, 20'마켓팅부'60'전산부'90'경영부''부서'from employees; 
    -- department_id 를 출력할건데 20은 마켓팅부 60은 전산부 90은 경영부 나머지는 부서로 출력하겠다
     
    -- case() if문과 같은 역할
    select first_name , department_id ,
     case when department_id = 20 then '마켓팅부'
             when department_id = 60 then '전산실'
             when department_id = 90 then '경영부'
             else ''
             end "부서명"
    from employees
    order by department_id;
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

    nvl() 을 where절에서 쓰는 경우가 안나와서 아쉬움.. where절에서 쓰일 때가 아직 이해가 잘 안된당..

    case() 문을 쓸 때는 

    case when 조건 then 결과

          ...

          else 결과

          end

     

    ** decode() 결과

     

    **case() 결과

     

    'DataBase > oracle' 카테고리의 다른 글

    oracle join  (0) 2020.03.16
    oracle group by , having , rollup  (0) 2020.03.15
    oracle 문자형 함수  (0) 2020.03.15
    oracle 숫자형 함수  (0) 2020.03.15
    Select  (0) 2020.03.10

    댓글

Designed by Tistory.