6강
--함수
--1. 단일행함수
--1.1 숫자함수/문자함수/날짜함수/변환함수/일반함수
--숫자함수? ROUND(반올림) TRUNC(소수점 버리는 함수) CEIL(소수점 큰수에 붙음) FLOOR(작은수에 붙음) MOD(10,3)=1 ABS(그 값으로)
SELECT ROUND(345.678),ROUND(345.678,0),ROUND(345.678,1), ROUND(345.678,-1)
FROM DUAL;
SELECT CEIL(3.01),CEIL(-3.01),FLOOR(3.01),FLOOR(-3.01)
FROM DUAL;
SELECT TRUNC(123.456),TRUNC(123.456,2)
FROM DUAL;
SELECT MOD(10,3)
FROM DUAL;
SELECT ABS(-100)
FROM DUAL;
--문자함수
--LOWER(소문자로)/UPPER(대문자로)/INITCAP(앞자만대문자로)/CONCAT(이름+직업)/SUCSTR/LENGTH/INSTR
SELECT LOWER('KOREA')
FROM DUAL;
SELECT UPPER('KOREA')
FROM DUAL;
SELECT INITCAP('ABCD')
FROM DUAL;
SELECT CONCAT(ENAME,JOB)
FROM EMP;
SELECT ENAME,SUBSTR(ENAME,2,3)
FROM EMP;
SELECT ENAME,LENGTH(ENAME)
FROM EMP;
SELECT ENAME,INSTR(ENAME,'A',2)
FROM EMP;
--날짜함수
--SYSDATE(오늘날짜)/MONTHS_BETWEEN/NEXT_DAY/ADD_MONTHS(현재달에 더하기)/LAST_DAY(이달의 마지막날)
SELECT SYSDATE
FROM DUAL;
SELECT ENAME,HIREDATE,ROUND(MONTHS_BETWEEN(SYSDATE,HIREDATE))
FROM EMP;
SELECT ADD_MONTHS(SYSDATE,5)
FROM DUAL;
SELECT LAST_DAY(SYSDATE)
FROM DUAL;
SELECT LAST_DAY(SYSDATE+10)
FROM DUAL;
--변환함수
--TO_NUMBER/TO_DATE/TO_CHAR
--TO_CHAR
SELECT EMPNO,ENAME,TO_CHAR(HIREDATE,'YYYY-MM-DD-DAY:AM:HH:MI:SS') AS 입사년월
FROM EMP;
SELECT EMPNO,ENAME,HIREDATE FROM EMP;
SELECT TO_CHAR(TO_NUMBER(1234.5678),'9999.999') RLT
FROM DUAL;
SELECT EMPNO,ENAME,TO_CHAR(TO_NUMBER(SAL),'$9999.9') SALARY
FROM EMP;
SELECT EMPNO,ENAME
FROM EMP
WHERE HIREDATE = TO_DATE('1980-12-17','YY-MM-DD');
SELECT * FROM EMP
WHERE ENAME='SMITH';
--일반함수
--NVL/NVL2/NULLIE(인수1,인수2)인수1이랑2가같으면 NULL값 다르면 그대로 인수1/COALESCE
SELECT ENAME,NVL(MGR,0)
FROM EMP;
SELECT ENAME,SAL,COMM,NVL2(COMM,SAL+COMM,SAL) AS SALARY
FROM EMP;
SELECT ENAME,NVL2(COMM,SAL+COMM,SAL) AS SALARY
FROM EMP;
SELECT NVL(NULLIF('A','A'),'널값')
FROM DUAL;
SELECT COALESCE(NULL,NULL,NULL,10,100,200)
FROM DUAL;
--CASE,DECODE란?
SELECT EMPNO,ENAME,JOB,SAL,
CASE JOB WHEN 'SALESMAN' THEN SAL*1.1
WHEN 'CLERK' THEN SAL*1.15
WHEN 'MANAGER' THEN SAL*1.2
ELSE SAL
END AS 인센티브
FROM EMP;
SELECT EMPNO,ENAME,JOB,SAL,
DECODE(JOB,'SALESMAN',SAL*1.1,
'CLERK',SAL*1.15,
'MANAGER',SAL*1.2,
SAL) AS 인상된급여
FROM EMP;
SELECT ENAME,SAL FROM EMP
WHERE ROWNUM<=5
ORDER BY SAL DESC;
--136P
SELECT JOB,
SUBSTR(JOB,-LENGTH(JOB)),SUBSTR(JOB,2,3),SUBSTR(JOB,-3)
FROM EMP;
--P141
SELECT REPLACE('010-1234-1234','-','/') AS RLT
FROM DUAL;
--P140
SELECT RPAD('ORACLE',15,'#')
FROM DUAL;
SELECT TABLE_NAME FROM USER_TABLES;
--TRIM 함수 (문자를 삭제할때 사용)
SELECT '['||TRIM('__ORACLE__')||']' AS TRIM
FROM DUAL;
SELECT '['||TRIM(LEADING FROM '__ORACLE__')||']' AS TRIM_LEADING
FROM DUAL;
SELECT ENAME,TRIM('A' FROM ENAME) AS T_ENAME
FROM EMP;
--NVL2란?
SELECT * FROM EMP;
SELECT ENAME,EMPNO,NVL2(COMM,SAL+COMM,SAL) AS SALARY
FROM EMP
ORDER BY SALARY DESC;
'오라클 SQL' 카테고리의 다른 글
오라클 SQL 7강 연습문제 (0) | 2021.02.26 |
---|---|
오라클 SQL 6강 연습문제 (0) | 2021.02.26 |
오라클 SQL 7강 (0) | 2021.02.26 |
오라클 SQL 4~5강 (0) | 2021.02.26 |
오라클 SQL 1~3강 (0) | 2021.02.25 |