์ซ์ ๋ฌธ์ ๋ ์ง
NUMBER VARCHER2 DATE
TO_CHAR TO_CHAR
TO_NUMBER TO_DATE
=> TO_CHAR ๋ TO_NUMBER ๋ ์ฐ์ฐ์ด ๋ถ์ผ๋ฉด ๋ชจ๋ ์ซ์๋ก ์๋ ํ๋ณํ ํด์ค.. ์ซ์๋ฅผ ๋ฌธ์๋ก ์ฐ๋ ค๋ฉด || ์ฌ์ฉ!
SELECT
2+2,
TO_CHAR(2) + 123, --๋ฌธ์๋ก ๋ฐ๊พธ๊ฒ ๋ค ํ์ง๋ง ์๋ํ๋ณํ~!
TO_NUMBER('123123')+300 --์ซ์๋ก ๋ฐ๊พธ๊ฒ ๋ค
FROM dual;
๐ป
SELECT
sal,
sal+100,
sal+'100', --์ซ์๋ก ์๋ํ๋ณํ~!
sal || '100'
FROM emp;
๐ป
SELECT
TO_CHAR(SYSDATE, 'YYYY-MM-DD'), --ํ์ฌ์๊ฐ์ ์ด ํํ๋ก ๋ฐ๊พธ๊ฒ ๋ค
TO_CHAR(SYSDATE, 'YYYY/MM/DD'),
TO_CHAR(SYSDATE, 'YY/MM/DD'),
TO_CHAR(SYSDATE, 'MM-DD'),
TO_CHAR(SYSDATE, 'DD HH:MI:SS')
FROM dual;
๐ป
SELECT
TO_CHAR(hiredate, 'YY/MM/DD') --hiredate๋ฅผ ์ด ํํ๋ก ๋ฐ๊พธ๊ฒ ๋ค
FROM emp;
๐ป
SELECT *
FROM emp
WHERE TO_CHAR(hiredate, 'YY') = '81'; --TO_CHAR(hiredate, 'YY')='81' ์ฐ๋๊ธฐ์ค์ผ๋ก 2์๋ฆฌ๋ง ๋ฝ๊ฒ ๋ค->81์ ๋ฝ์๋
๐ป
SELECT
studno,
name,
TO_CHAR(birthday, 'YYYY-MM-DD') birthday --birthday๋ฅผ ์ด ํํ๋ก ๋ฐ๊พธ๊ฒ ๋ค
FROM student
WHERE TO_CHAR(birthday, 'MM') = '01'; --์์ ๋ฝ์๋ด์ผํจ
๐ป
SELECT
empno,
ename,
TO_CHAR(hiredate, 'YYYY-MM-DD') hiredate,
TO_CHAR((sal*12)+comm, '$999,999') sal, --TO_CHAR(์ซ์, '$999,999') : ์ซ์ ์ถ๋ ฅ์ ์์ $๋ถ์ด์ ๊ธ์ก์๋ฆฟ์๋ง๋ค ,๋ถ์
TO_CHAR((sal*12)+comm + ((sal*12)+comm)*0.15, '$999,999') "15%์ธ์" --์๋์ฐ๋ด + ์ฐ๋ด15% / ์ฐ๋ด*1.15
FROM emp
WHERE comm IS NOT NULL;
๐ป
NVL() : NULL๊ฐ์ ๋ค๋ฅธ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ๋์ฒด (NULL VALUE)
NVL(ํน์ ์ปฌ๋ผ, ๊ธฐ๋ณธ ๋์ฒด๊ฐ)
NVL2() : NULL ๊ฐ ๋์ฒด
NVL2(ํน์ ์ปฌ๋ผ, ๋์ด์๋๋ฉด(๊ฐ์ด์์ผ๋ฉด), ๋์ด๋ฉด(๊ฐ์ด์์ผ๋ฉด)) == ์ผํญ์ฐ์ฐ์
SELECT
position,
NVL(position, 'member') --position ์ปฌ๋ผ์ null์ธ ๊ฒ๋ค์ ๊ธฐ๋ณธ๊ฐ์ member์ผ๋ก ์ง์ !
FROM emp2;
๐ป
SELECT
empno,
ename,
comm,
NVL2(comm, 'Exist', 'NULL') NVL2 --NULL์ด ์๋ ๊ฒฝ์ฐ(๊ฐ์ด ์๋ ๊ฒฝ์ฐ) Exist, NULL์ธ ๊ฒฝ์ฐ(๊ฐ์ด ์๋ ๊ฒฝ์ฐ) NULL
FROM emp
WHERE deptno = 30;
๐ป
DECODE(์ปฌ๋ผ, 100, '๋ง์ผ๋ฉด์ฌ๊ธฐ', '์๋๋ฉด์ฌ๊ธฐ') --if ์ปฌ๋ผ์ด 100์ด๋ฉด, ์๋๋ฉด
DECODE(์ปฌ๋ผ, ์ด ๊ฐ๊ณผ ๋์ผํ๋ฉด, '๋ง์ผ๋ฉด์ฌ๊ธฐ') ์๋๊ฒฝ์ฐ๋ ์๋์ผ๋ก NULL
DECODE(์ปฌ๋ผ, 100, (DECODE ์ปฌ๋ผ, 200, A), DECODE(์ปฌ๋ผ, 300, C)) --์ปฌ๋ผ์ด 100์ด๋ฉด (DECODE ์ปฌ๋ผ, 200, A) 100์ด ์๋๋ฉด DECODE(์ปฌ๋ผ, 300, C)
SELECT
name,
SUBSTR(jumin, 1, 6) || '-' || SUBSTR(jumin, 7, 11) jumin,
DECODE(SUBSTR(jumin, 7, 1), '1', '๋จ์', '2', '์ฌ์') ์ฑ๋ณ
FROM student
WHERE deptno1 = 101;
๐ป
SELECT
name,
tel,
DECODE(SUBSTR(tel, 1, INSTR(tel, ')')-1) , '02', '์์ธ', '031', '๊ฒฝ๊ธฐ', '051', '๋ถ์ฐ', '052', '์ธ์ฐ', '055', '๊ฒฝ๋จ', '053', '๋๊ตฌ', NULL) ์ง์ญ๋ช
FROM student
WHERE deptno1 = 101;
๐ป