[목차]

7. 함수

    1) 문자처리 함수 
        ① length() 
        ② sum(), max(), min(), count(), avg(), count(*) 
        ③ lower(), initcap(),upper() 
        ④ dual 
        ⑤ concat(A,B)
        ⑥ instr('문자열', '찾을 문자',m번째 부터 검색, 찾은 문자의 n번째 위치)  
        ⑦ substr('문자열', m번쨰 부터 짜름, n개 글자) 
        ⑧ replace('문자열', 바꿀문자, 바뀔문자) 
        ⑨ lpad('문자열', 전체 자리수, 공백문자)  
            rpad
        ⑩ ltrim('문자열', '지울문자') 
            rtrim
            trim
    2) 숫자자리 함수  
        ① round() 
        ② ceil() 
        ③ floor() 
    3) 날짜 함수  
        ① sysdate 
        ② months_between(A,B)  
        ③ add_month(A,B)  
        ④ next_day(A,B) 
        ⑤ last_day() 
    4) 변환함수  
        ① to_char(A,'9.99')  
        ② to_char(A,'YYYY-MM-DD')  
    5) 조건함수  
        ① decode(칼럼, 값1, 동일하면 출력할 값1 
                             값2, 동일하면 출력할 값2 
                       나머지 경우 출력할 값  )  
        ② case when 조건식1 then  출력할 값1 
            when 조건식2 then 출력할 값2 
            else 나머지 경우 출력할 값 
            end 별칭 
    6) null처리 함수  
        ① nvl(A,B)  
        ② nvl2(A,B)  

 

[내용]

7. 함수 
    1) 문자처리 함수

        ① length()

select ename, length(ename)
where length(ename)>=6;

        ② sum(sal), max(), min(), count(), avg(), count(*)

select ename, sal, max(sal)
from emp
group by ename;
select deptno, count(empno)
from emp
group by deptno;


        ③ lower(), initcap(첫글자만 대문자),upper()

select empno, ename, lower(job), deptno
from emp
where ename = 'SCOTT'


        ④ dual : test 1행짜리 테이블.

select *
from dual; 


        ⑤ concat(A,B) : || 문자열 연결. 
            concat(A, concat(B,C))

select concat(ename,concat('의 급여',concat(sal,'만원')))
from emp
where sal<1000;


        ⑥ instr('문자열', '찾을 문자',m번째 부터 검색, 찾은 문자의 n번째 위치) 
           -> 위치반환


         substr('문자열', m번쨰 부터 짜름, n개 글자) 
           -> 글자 추출 -1은 맨뒤.

select ename, hiredate
from emp
where substr(hiredate,1,2)=81;
select empno, ename, job, sal, deptno
from emp
where substr(ename,1,1) > 'K' and substr(ename,1,1) < 'Y';


        ⑧ replace('문자열', 바꿀문자, 바뀔문자)


        ⑨ lpad('문자열', 전체 자리수, 공백문자) 
           -> 왼쪽에 공백문자를 가짐, 전체 자리수에서 부족한 공백은 설정한 공백문자로 채운다.

            rpad : 오른쪽에 공백문자를 가짐.

select ename, job, lpad(sal,5,'*') as sal
from emp
where sal<=2000;

 

          ⑩ ltrim('문자열', '지울문자') : 왼쪽 문자를 지움

              rtrim('문자열', '지울문자') : 오른쪽 문자를 지움 
              trim(''from'문자열') : 양쪽 문자를 지움 

select ename, job, ltrim(lpad(sal,5,'*'),'*') as sal
from emp
where sal<=2000;
select ltrim(job,'A'), ltrim(sal,1)
from emp
where deptno=10;


    2) 숫자자리 함수 
         round() : 반올림

select deptno, round(avg(sal)) avg
from emp
where job<>'PRESIDENT'
group by deptno
having avg(sal)>1800
order by deptno;


         ceil()  : 올림 
         floor   : 버림 

    3) 날짜 함수 
        ① sysdate : 오늘날짜. 연산가능 
         months_between(최신날짜, 먼날짜) 
         add_month(,더할달수) 
        ④ next_day(지정일,'금') 요일 
        ⑤ last_day(지정일) : 마지막날

select e.first_name, e.salary, e.hire_date, d.department_name
from employees e, departments d
where e.department_id=d.department_id
      and months_between(sysdate,hire_date) >12*18;


    4) 변환함수 
         to_char(   ,'9.99') 
         to_char(   ,'YYYY-MM-DD')

select to_char(hiredate,'MM')월, count(*)입사자수
from emp
group by to_char(hiredate,'MM')
order by to_char(hiredate,'MM');


    5) 조건함수 
         decode(칼럼, 값1, 동일하면 출력할 값1

                             값2, 동일하면 출력할 값2

                       나머지 경우 출력할 값  )

select ename, deptno, decode(deptno, 10, '전산실',
                              20, '총무과',
                              30, '기획실',
                              '신입')
from emp;


         case when 조건식1 then  출력할 값1
            when 조건식2 then 출력할 값2
            else 나머지 경우 출력할 값
            end 별칭

select ename, sal, case when sal>=5000 then '1등급'
                 when sal>=2000 and sal<5000 then '2등급'
                 when sal>=1000 and sal<2000 then '3등급'
                 else '신입'
                 end  등급표
from emp;


    6) null처리 함수 
        ① nvl(칼럼or표현식, null인 경우 적용할 값 or 표현식(연산,함수호출))

select nvl(to_char(department_id),'No Department')부서번호, round(avg(salary),0)평균급여
from employees
group by department_id
having avg(salary) >6000;


        ② nvl2(칼럼 or 표현식, null이 아닌 경우 적용할 값, null인 경우 적용할 값)

select ename, mgr, nvl2(mgr,'담당','상위자') 관리자
from emp
order by 관리자;

+ Recent posts

1