๐Ÿ’โ€โ™€๏ธ ํ•จ์ˆ˜(Function)๋ž€,
ํ•˜๋‚˜์˜ ํฐ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ถ€๋ถ„๋“ค์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์ž‘์„ฑํ•ด ๋†“์€
์ž‘์€ ์„œ๋ธŒ ํ”„๋กœ๊ทธ๋žจ

  • ์ปฌ๋Ÿผ ๊ฐ’์„ ์ฝ์–ด์„œ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ด

๐Ÿ’โ€โ™€๏ธ ๊ทธ๋ฃน(GROUP)ํ•จ์ˆ˜ & ๋‹จ์ผํ–‰(SINGLE ROW)ํ•จ์ˆ˜๋ž€,

  • ๊ทธ๋ฃน(GROUP)ํ•จ์ˆ˜ : ์ปฌ๋Ÿผ์— ๊ธฐ๋ก๋œ N๊ฐœ์˜ ๊ฐ’์„ ์ฝ์–ด์„œ ํ•œ ๊ฐœ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ด
  • ๋‹จ์ผํ–‰(SINGLE ROW)ํ•จ์ˆ˜ : ์ปฌ๋Ÿผ์— ๊ธฐ๋ก๋œ N๊ฐœ์˜ ๊ฐ’์„ ์ฝ์–ด์„œ N๊ฐœ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ด
  • ๋‘ ํ•จ์ˆ˜๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ํ–‰์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋‹ค๋ฅด๋ฏ€๋กœ SELECT์ ˆ์— ๋‹จ์ผํ–‰ ํ•จ์ˆ˜์™€ ๊ทธ๋ฃน ํ•จ์ˆ˜๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ

๐Ÿ‘€ ๊ทธ๋ฃน ํ•จ์ˆ˜

๐Ÿ“ ๊ทธ๋ฃน ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜

1) SUM
2) AVG
3) MAX
4) MIN
5) COUNT

1) SUM

SUM(์ˆซ์ž๊ฐ€ ๊ธฐ๋ก๋œ ์ปฌ๋Ÿผ๋ช…) : ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•˜์—ฌ ๋ฆฌํ„ด

SELECT
		SUM(SALARY)
   FROM EMPLOYEE;

2) AVG

AVG(์ˆซ์ž๊ฐ€ ๊ธฐ๋ก๋œ ์ปฌ๋Ÿผ๋ช…) : ํ‰๊ท ์„ ๊ตฌํ•˜์—ฌ ๋ฆฌํ„ด

SELECT
		AVG(SALARY)
   FROM EMPLOYEE;

3) MAX

MAX(์ปฌ๋Ÿผ๋ช…) : ์ปฌ๋Ÿผ์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’ ๋ฆฌํ„ด. ์ทจ๊ธ‰ํ•˜๋Š” ์ž๋ฃŒํ˜•์€ ANY TYPE.

SELECT
		MAX(EMAIL)
      , MAX(HIRE_DATE)
      , MAX(SALARY)
   FROM EMPLOYEE;

4) MIN

MIN(์ปฌ๋Ÿผ๋ช…) : ์ปฌ๋Ÿผ์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ ๋ฆฌํ„ด. ์ทจ๊ธ‰ํ•˜๋Š” ์ž๋ฃŒํ˜•์€ ANY TYPE.

SELECT
        MIN(EMAIL)
      , MIN(HIRE_DATE)
      , MIN(SALARY)
   FROM EMPLOYEE;

5) COUNT

COUNT(* or ์ปฌ๋Ÿผ๋ช…) : ํ–‰์˜ ๊ฐฏ์ˆ˜๋ฅผ ํ—ค์•„๋ ค์„œ ๋ฆฌํ„ด
COUNT(*) : NULL์„ ํฌํ•จํ•œ ์ „์ฒด ํ–‰ ๊ฐฏ์ˆ˜ ๋ฆฌํ„ด
COOUNT(์ปฌ๋Ÿผ๋ช…) : NULL์„ ์ œ์™ธํ•œ ์‹ค์ œ ๊ฐ’์ด ๊ธฐ๋ก๋œ ํ–‰ ๊ฐฏ์ˆ˜ ๋ฆฌํ„ด
COUNT(DISTINCT ์ปฌ๋Ÿผ๋ช…) : ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ํ–‰ ๊ฐฏ์ˆ˜ ๋ฆฌํ„ด

SELECT
        COUNT(*) 
    ,   COUNT(DEPT_CODE)			>>> NULL ์ œ์™ธํ•œ ๊ฐ’์˜ ํ–‰ ๊ฐฏ์ˆ˜
    ,   COUNT(DISTINCT DEPT_CODE)	>>> ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ํ–‰ ๊ฐฏ์ˆ˜
   FROM EMPLOYEE;

๐Ÿ‘€ ๋‹จ์ผํ–‰ ํ•จ์ˆ˜

๐Ÿ‘‰ ๋ฌธ์ž ๊ด€๋ จ ํ•จ์ˆ˜

๐Ÿ“ ๋ฌธ์ž ๊ด€๋ จ ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜

1) LENGTH / LENGTHB
2) INSTR
3) LPAD / RPAD
4) LTRIM / RTRIM
5) TRIM
6) SUBSTR
7) SUBSTRB
8) LOWER / UPPER / INITCAP
9) CONCAT
10) REPLACE

1) LENGTH / LENGTHB

๐Ÿ’โ€โ™€๏ธ LENGTH / LENGTHB๋ž€,
์ฃผ์–ด์ง„ ์ปฌ๋Ÿผ ๊ฐ’ or ๋ฌธ์ž์—ด์˜ ๊ธธ์ด(byte)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

SELECT
       LENGTH('์˜ค๋ผํด')
     , LENGTHB('์˜ค๋ผํด') >>> LENGTHB : byteํฌ๊ธฐ๋ฅผ ํฌํ•จํ•œ ๊ธธ์ด
     					>>> ํ•œ๊ธ€์€ ๊ธ€์ž ๋‹น 3byte์ด๋ฏ€๋กœ 9 ์ถœ๋ ฅ
  FROM DUAL;
SELECT
       LENGTH(EMAIL)	>>> ์ด๋ฉ”์ผ(์˜๋ฌธ)์˜ ๊ธธ์ด์ธ 17์ถœ๋ ฅ
     , LENGTHB(EMAIL)	>>> ์˜๋ฌธ์€ ๊ธ€์ž๋‹น 1byte
  FROM EMPLOYEE;

2) INSTR

๐Ÿ’โ€โ™€๏ธ INSTR๋ž€,
์ฐพ๋Š” ๋ฌธ์ž(์—ด)์ด ์ง€์ •ํ•œ ์œ„์น˜๋ถ€ํ„ฐ ์ง€์ •ํ•œ ํšŸ์ˆ˜๋งŒํผ ๋‚˜ํƒ€๋‚œ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

INSTR('๋ฌธ์ž์—ด'or ์ปฌ๋Ÿผ๋ช…, '๋ฌธ์ž', ์ฐพ์„ ์œ„์น˜์˜ ์‹œ์ž‘๊ฐ’, ๋นˆ๋„)

SELECT
       EMAIL
     , INSTR(EMAIL, '@', -1) ์œ„์น˜
  FROM EMPLOYEE; 
  
SELECT INSTR('AABAACAABBAA', 'B') FROM DUAL;
SELECT INSTR('AABAACAABBAA', 'B', 1) FROM DUAL;  
SELECT INSTR('AABAACAABBAA', 'B', -1) FROM DUAL;
SELECT INSTR('AABAACAABBAA', 'B', 1, 2) FROM DUAL; 	>>> 'AABAACAABBAA'์—์„œ ๋‘ ๋ฒˆ์งธ B๋ฅผ 1๋ฒˆ ์ž๋ฆฌ์—์„œ๋ถ€ํ„ฐ ์ฐพ๊ธฐ
SELECT INSTR('AABAACAABBAA', 'B', -1, 2) FROM DUAL;	>>> 'AABAACAABBAA'์—์„œ ๋‘ ๋ฒˆ์งธ B๋ฅผ -1๋ฒˆ ์ž๋ฆฌ์—์„œ๋ถ€ํ„ฐ ์ฐพ๊ธฐ (๋งจ ๋’ค์—์„œ ๊ฐ€๊นŒ์šด ์ˆœ)

3) LPAD / RPAD

๐Ÿ’โ€โ™€๏ธ LPAD / RPAD๋ž€,
์ฃผ์–ด์ง„ ์ปฌ๋Ÿผ ๋ฌธ์ž์—ด์— ์ž„์˜์˜ ๋ฌธ์ž์—ด์„ ๋ง๋ถ™์—ฌ ๊ธธ์ด N์˜ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

SELECT
       LPAD(EMAIL, 20, '#')	>>> ์ด๋ฉ”์ผ์„ 20๊ธ€์ž๋กœ ์ฑ„์šฐ๋Š”๋ฐ, ๋นˆ ๊ณณ์€ '#'์œผ๋กœ ์ฑ„์šฐ๊ธฐ (์™ผ์ชฝ) 
  FROM EMPLOYEE;
SELECT
       RPAD(EMAIL, 20, '#')	>>> ์ด๋ฉ”์ผ์„ 20๊ธ€์ž๋กœ ์ฑ„์šฐ๋Š”๋ฐ, ๋นˆ ๊ณณ์€ '#'์œผ๋กœ ์ฑ„์šฐ๊ธฐ (์˜ค๋ฅธ์ชฝ) 
  FROM EMPLOYEE;  
SELECT
       LPAD(EMAIL, 10)	>>> ์ด๋ฉ”์ผ์ด 10๊ธ€์ž๊นŒ์ง€๋งŒ ์ถœ๋ ฅ๋˜๊ณ  ๋’ค๋Š” ์ž˜๋ฆผ
  FROM EMPLOYEE;
SELECT
       RPAD(EMAIL, 10) >>> ์œ„์™€ ๊ฐ™์Œ
  FROM EMPLOYEE;

4) LTRIM / RTRIM

๐Ÿ’โ€โ™€๏ธ LTRIM / RTRIM์ด๋ž€,
์ฃผ์–ด์ง„ ์ปฌ๋Ÿผ์ด๋‚˜ ๋ฌธ์ž์—ด ์™ผ์ชฝ/์˜ค๋ฅธ์ชฝ์—์„œ ์ง€์ •ํ•œ ๋ฌธ์ž ํ˜น์€ ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•œ ๋‚˜๋จธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

SELECT LTRIM('   GREEDY') FROM DUAL;
SELECT LTRIM('   GREEDY', ' ') FROM DUAL; >>> ๊ณต๋ฐฑ์„ ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ œ๊ฑฐ
SELECT LTRIM('000123456', '0') FROM DUAL; >>> 0์„ ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ œ๊ฑฐ
SELECT LTRIM('123123GREEDY', '123') FROM DUAL;
SELECT LTRIM('132123GREEDY123', '123') FROM DUAL;	>>> 1 or 2 or 3์„ ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ œ๊ฑฐ (๋งจ ์˜ค๋ฅธ์ชฝ 123์€ ์ œ๊ฑฐ X)
SELECT LTRIM('ACABACGREEDY', 'ABC') FROM DUAL;		>>> A or B or C๋ฅผ ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ œ๊ฑฐ
SELECT LTRIM('5782GREEDY', '0123456789') FROM DUAL;	>>> ์™ผ์ชฝ์— ์žˆ๋Š” ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ์ œ๊ฑฐ
SELECT RTRIM('GREEDY   ') FROM DUAL;
SELECT RTRIM('GREEDY   ', ' ') FROM DUAL;
SELECT RTRIM('123456000', '0') FROM DUAL;
SELECT RTRIM('GREEDY123123', '123') FROM DUAL;
SELECT RTRIM('123123GREEDY123', '123') FROM DUAL; >>> 1 or 2 or 3์„ ์˜ค๋ฅธ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ œ๊ฑฐ (๋งจ ์™ผ์ชฝ 123123์€ ์ œ๊ฑฐ X)
SELECT RTRIM('GREEDYACABAC', 'ABC') FROM DUAL;
SELECT RTRIM('GREEDY5782', '0123456789') FROM DUAL;

5) TRIM

๐Ÿ’โ€โ™€๏ธ TRIM์ด๋ž€,
์ฃผ์–ด์ง„ ์ปฌ๋Ÿผ์ด๋‚˜ ๋ฌธ์ž์—ด์˜ ์•ž/๋’ค์— ์ง€์ •ํ•œ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐ

SELECT TRIM('   GREEDY   ') FROM DUAL;
SELECT TRIM('Z' FROM 'ZZZGREEDYZZZ') FROM DUAL;		>>> 'ZZZGREEDYZZZ'์œผ๋กœ๋ถ€ํ„ฐ Z๋ชจ๋‘ ์ œ๊ฑฐ
SELECT TRIM(LEADING 'Z' FROM 'ZZZGREEDYZZZ') FROM DUAL;		>>> ์™ผ์ชฝ์˜ Z๋งŒ ์ œ๊ฑฐ
SELECT TRIM(TRAILING 'Z' FROM 'ZZZGREEDYZZZ') FROM DUAL;	>>> ์˜ค๋ฅธ์ชฝ์˜ Z๋งŒ ์ œ๊ฑฐ
SELECT TRIM(BOTH 'Z' FROM 'ZZZGREEDYZZZ') FROM DUAL;		>>> ์–‘์ชฝ์˜ Z๋งŒ ์ œ๊ฑฐ

6) SUBSTR

๐Ÿ’โ€โ™€๏ธ SUBSTR์ด๋ž€,
์ปฌ๋Ÿผ์ด๋‚˜ ๋ฌธ์ž์—ด์—์„œ ์ง€์ •ํ•œ ์œ„์น˜๋กœ๋ถ€ํ„ฐ ์ง€์ •ํ•œ ๊ฐฏ์ˆ˜์˜ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ์„œ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

SELECT SUBSTR('SHOWMETHEMONEY', 5, 2) FROM DUAL;	>>> ๋‹ค์„ฏ๋ฒˆ์งธ ์ž๋ฆฌ๋ถ€ํ„ฐ 2๊ธ€์ž ์ถœ๋ ฅ
SELECT SUBSTR('SHOWMETHEMONEY', 7) FROM DUAL;		>>> ์ผ๊ณฑ๋ฒˆ์งธ ์ž๋ฆฌ๋ถ€ํ„ฐ ๋๊นŒ์ง€
SELECT SUBSTR('SHOWMETHEMONEY', -8, 3) FROM DUAL;	>>> ๋’ค์—์„œ ์—ฌ๋Ÿ๋ฒˆ์งธ ์ž๋ฆฌ๋ถ€ํ„ฐ 3๊ธ€์ž ์ถœ๋ ฅ
SELECT SUBSTR('์‡ผ์šฐ ๋ฏธ ๋” ๋จธ๋‹ˆ', 2, 5) FROM DUAL;		>>> ํ•œ๊ธ€๋„ ์˜๋ฌธ์ฒ˜๋Ÿผ ํ•œ ๊ธ€์ž์”ฉ ์ทจ๊ธ‰๋˜์–ด '์šฐ ๋ฏธ ๋”'์ถœ๋ ฅ

โ—ผ ์ง์›๋“ค์˜ ์ฃผ๋ฏผ๋ฒˆํ˜ธ๋ฅผ ์กฐํšŒํ•˜์—ฌ ์‚ฌ์›๋ช…, ์ƒ๋…„, ์ƒ์›”, ์ƒ์ผ์„ ๊ฐ๊ฐ ๋ถ„๋ฆฌํ•˜์—ฌ ์กฐํšŒ (๋‹จ, ์ปฌ๋Ÿผ์˜ ๋ณ„์นญ์€ ์‚ฌ์›๋ช…, ์ƒ๋…„, ์ƒ์›”, ์ƒ์ผ)

SELECT
        EMP_NAME ์‚ฌ์›๋ช…
    ,   SUBSTR(EMP_NO, 1, 2) ์ƒ๋…„
    ,   SUBSTR(EMP_NO, 3, 2) ์ƒ์›”
    ,   SUBSTR(EMP_NO, 5, 2) ์ƒ์ผ
   FROM EMPLOYEE;

โ—ผ ์ง์›๋“ค์˜ ์ž…์‚ฌ์ผ์—์„œ ์ž…์‚ฌ๋…„๋„, ์ž…์‚ฌ์›”, ์ž…์‚ฌ ๋‚ ์งœ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ์กฐํšŒ (๋‚ ์งœ ๋ฐ์ดํ„ฐ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

SELECT
        HIRE_DATE
    ,   SUBSTR(HIRE_DATE, 1, 2) ์ž…์‚ฌ๋…„๋„
    ,   SUBSTR(HIRE_DATE, 4, 2) ์ž…์‚ฌ์›”
    ,   SUBSTR(HIRE_DATE, 7, 2) ์ž…์‚ฌ์ผ
   FROM EMPLOYEE;

โ—ผ EMP_NO๋ฅผ ํ†ตํ•ด ์„ฑ๋ณ„์„ ํŒ๋‹จํ•˜์—ฌ ์—ฌ์„ฑ ์ง์›๋“ค์˜ ๋ชจ๋“  ์ปฌ๋Ÿผ ์ •๋ณด๋ฅผ ์กฐํšŒ (WHERE์ ˆ์—์„œ๋„ ํ•จ์ˆ˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

SELECT
		*
   FROM EMPLOYEE
  WHERE SUBSTR(EMP_NO, 8, 1) = '2';

๐Ÿ˜ˆ Warning WHERE์ ˆ์—๋Š” ๋‹จ์ผํ–‰ ํ•จ์ˆ˜๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

SELECT
        *
   FROM EMPLOYEE
  WHERE AVG(SALARY) > 100;
    >>> '๊ทธ๋ฃน ํ•จ์ˆ˜๋Š” ํ—ˆ๊ฐ€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค' ์˜ค๋ฅ˜ (์กฐ๊ฑด์„ ํŠน์ •ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ)

โ—ผ ์‚ฌ์›๋ช…, ์ฃผ๋ฏผ๋ฒˆํ˜ธ ์กฐํšŒ. ์ฃผ๋ฏผ๋ฒˆํ˜ธ๋Š” ์ƒ๋…„์›”์ผ๋งŒ ๋ณด์ด๊ฒŒ ํ•˜๊ณ  '-' ๋‹ค์Œ์˜ ๊ฐ’์€ '*'๋กœ ๋ฐ”๊ฟ” ์ถœ๋ ฅ (ํ•จ์ˆ˜ ์ค‘์ฒฉ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

SELECT
        EMP_NAME
    ,   RPAD(SUBSTR(EMP_NO, 1, 7), 14, '*') 
    	>>> 1 ~ 7 ์ž๋ฆฌ๊นŒ์ง€๋งŒ ์ž˜๋ผ๋‚ด๊ณ  ์ „์ฒด๋ฅผ 14์ž๋ฆฌ๋กœ ๊ณ ์ •ํ•˜๋ฉฐ ๋‚จ์€ ์ž๋ฆฌ๋Š” *๋กœ ์ฑ„์šฐ๊ธฐ
   FROM EMPLOYEE;

โ—ผ ์‚ฌ์›๋ช…, ์ด๋ฉ”์ผ, ์ด๋ฉ”์ผ์˜ @ ์ดํ›„๋ฅผ ์ œ์™ธํ•œ ์•„์ด๋”” ์กฐํšŒ

SELECT
        EMP_NAME
    ,   EMAIL
    ,   SUBSTR(EMAIL, 1, INSTR(EMAIL, '@')-1) ์•„์ด๋”” -- -1 : @์˜ ์•ž๊นŒ์ง€
   FROM EMPLOYEE;

7) SUBSTRB

๐Ÿ’โ€โ™€๏ธ SUBSTRB๋ž€,
๋ฌธ์ž์—ด์„ ์ž˜๋ผ์„œ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

SELECT 
       SUBSTR('ORACLE', 3, 2)
     , SUBSTRB('ORACLE', 3, 2)	>>> ์œ„์™€ ์ฐจ์ด๊ฐ€ ์—†์Œ(์˜๋ฌธ์€ 1byte)
  FROM DUAL;
SELECT 
       SUBSTR('์˜ค๋ผํด', 2, 2)
     , SUBSTRB('์˜ค๋ผํด', 4, 6)	>>> 4๋ฒˆ์งธ ์ž๋ฆฌ์ธ '๋ผ'๋ถ€ํ„ฐ 6byte ์ถœ๋ ฅ
     							>>> '๋ผํด' ์ถœ๋ ฅ
  FROM DUAL;  

8) LOWER / UPPER / INITCAP

๐Ÿ’โ€โ™€๏ธ LOWER / UPPER / INITCAP์ด๋ž€,
์ปฌ๋Ÿผ์˜ ๋ฌธ์ž ํ˜น์€ ๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž/๋Œ€๋ฌธ์ž/์ฒซ ๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

LOWER(๋ฌธ์ž์—ด or ์ปฌ๋Ÿผ) : ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

SELECT
       LOWER('Welcome To My World')
  FROM DUAL;

UPPER(๋ฌธ์ž์—ด or ์ปฌ๋Ÿผ) : ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

SELECT
       UPPER('Welcome To My World')
  FROM DUAL;

INITCAP(๋ฌธ์ž์—ด or ์ปฌ๋Ÿผ) : ์•ž ๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

SELECT
       INITCAP('welcome to my world')
  FROM DUAL;

9) CONCAT

๐Ÿ’โ€โ™€๏ธ CONCAT์ด๋ž€,
๋ฌธ์ž์—ด ํ˜น์€ ์ปฌ๋Ÿผ ๋‘ ๊ฐœ๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ํ•˜๋‚˜๋กœ ํ•ฉ์นœ ๋’ค, ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

SELECT
       CONCAT('๊ฐ€๋‚˜๋‹ค๋ผ', 'ABCD')
  FROM DUAL;
SELECT
       '๊ฐ€๋‚˜๋‹ค๋ผ' || 'ABCD'		>>> ์œ„์™€ ๋™์ผ (๋ณ‘ํ•ฉ)
  FROM DUAL;

10) REPLACE

๐Ÿ’โ€โ™€๏ธ REPLACE๋ž€,
์ปฌ๋Ÿผ ํ˜น์€ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ ๋ฐ›์•„ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•˜๋Š” ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พผ ๋’ค, ๋ฆฌํ„ด

SELECT
       REPLACE('์„œ์šธ์‹œ ๊ฐ•๋‚จ๊ตฌ ์—ญ์‚ผ๋™', '์—ญ์‚ผ๋™', '์‚ผ์„ฑ๋™') 
       					>>> ๋ฌธ์ž์—ด ์•ˆ์˜ ์—ญ์‚ผ๋™์„ ์‚ผ์„ฑ๋™์œผ๋กœ ๋ณ€๊ฒฝ
  FROM DUAL;

๐Ÿ‘‰ ์ˆซ์ž ์ฒ˜๋ฆฌ ํ•จ์ˆ˜

๐Ÿ“ ์ˆซ์ž ์ฒ˜๋ฆฌ ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜

1) ABS
2) MOD
3) ROUND
4) FLOOR
5) TRUNC
6) CEIL

1) ABS

ABS(์ˆซ์ž or ์ˆซ์ž๋กœ ๋œ ์ปฌ๋Ÿผ๋ช…) : ์ ˆ๋Œ€๊ฐ’ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜

SELECT
       ABS(-10)
     , ABS(10)
  FROM DUAL;

2) MOD

MOD(์ˆซ์ž or ์ˆซ์ž๋กœ ๋œ ์ปฌ๋Ÿผ๋ช…, ์ˆซ์ž or ์ˆซ์ž๋กœ ๋œ ์ปฌ๋Ÿผ๋ช…)
: ๋‘ ์ˆ˜๋ฅผ ๋‚˜๋ˆ„์–ด์„œ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ (์ฒ˜์Œ ์ธ์ž๋Š” ๋‚˜๋ˆ„์–ด์ง€๋Š” ์ˆ˜, ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ๋‚˜๋ˆŒ ์ˆ˜)

SELECT 
       MOD(10, 5)
     , MOD(10, 3) >>> 10์„ 3์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ ์ถœ๋ ฅ
  FROM DUAL;

3) ROUND

ROUND(์ˆซ์ž or ์ˆซ์ž๋กœ ๋œ ์ปฌ๋Ÿผ๋ช…, ์œ„์น˜) : ๋ฐ˜์˜ฌ๋ฆผํ•ด์„œ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

SELECT ROUND(123.456) FROM DUAL;
SELECT ROUND(123.456, 0) FROM DUAL;
SELECT ROUND(123.456, 1) FROM DUAL;		>>> ์†Œ์ˆ˜์  ์ฒซ๋ฒˆ์งธ์ž๋ฆฌ๊นŒ์ง€ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ์ถœ๋ ฅ
SELECT ROUND(123.456, 2) FROM DUAL;
SELECT ROUND(123.456, -2) FROM DUAL;	>>> 10์˜ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ์ถœ๋ ฅ
										>>> 100์ถœ๋ ฅ

4) FLOOR

FLOOR(์ˆซ์ž or ์ˆซ์ž๋กœ ๋œ ์ปฌ๋Ÿผ๋ช…) : ๋‚ด๋ฆผ์ฒ˜๋ฆฌ ํ•จ์ˆ˜

SELECT FLOOR(123.456) FROM DUAL;
SELECT FLOOR(123.678) FROM DUAL;	>>> ๋ฌด์กฐ๊ฑด ๋‚ด๋ฆผ

5) TRUNC

TRUNC(์ˆซ์ž or ์ˆซ์ž๋กœ ๋œ ์ปฌ๋Ÿผ๋ช…, ์œ„์น˜) : ๋‚ด๋ฆผ์ฒ˜๋ฆฌ ํ•จ์ˆ˜ (์œ„์น˜์ง€์ •๊ฐ€๋Šฅ)

SELECT TRUNC(123.456) FROM DUAL;
SELECT TRUNC(123.678) FROM DUAL;
SELECT TRUNC(123.456, 1) FROM DUAL;
SELECT TRUNC(123.456, 2) FROM DUAL;		>>> ๋ฐ˜์˜ฌ๋ฆผ์ด ์•„๋‹Œ ์†Œ์ˆ˜์  ๋‘๋ฒˆ์งธ์ž๋ฆฌ๊นŒ์ง€๋งŒ ๋‚จ๊ธฐ๊ณ  ์ž๋ฆ„
SELECT TRUNC(123.456, -1) FROM DUAL;	>>> 1์˜ ์ž๋ฆฌ์—์„œ ๋‚ด๋ฆผํ•˜์—ฌ ์ถœ๋ ฅ
										>>> 120 ์ถœ๋ ฅ

6) CEIL

CEIL(์ˆซ์ž or ์ˆซ์ž๋กœ ๋œ ์ปฌ๋Ÿผ๋ช…) : ์˜ฌ๋ฆผ์ฒ˜๋ฆฌ ํ•จ์ˆ˜

SELECT CEIL(123.456) FROM DUAL;		>>> ๋ฌด์กฐ๊ฑด ์˜ฌ๋ฆผ
SELECT CEIL(123.678) FROM DUAL; 

๐Ÿ‘‰ ๋‚ ์งœ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜

๐Ÿ“ ๋‚ ์งœ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜

1) SYSDATE
2) MONTHS_BETWEEN
3) ADD_MONTHS
4) NEXT_DAY
5) LAST_DAY
6) EXTRACT

1) SYSDATE

๐Ÿ’โ€โ™€๏ธ SYSDATE๋ž€,
์‹œ์Šคํ…œ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋‚ ์งœ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

SELECT SYSDATE FROM DUAL;	>>> ๋‚ ์งœ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‹œ๊ฐ„๋„ ์ถœ๋ ฅ๋จ

2) MONTHS_BETWEEN

MONTHS_BETWEEN(๋‚ ์งœ, ๋‚ ์งœ) : ๋‘ ๋‚ ์งœ์˜ ๊ฐœ์›” ์ˆ˜ ์ฐจ์ด๋ฅผ ์ˆซ์ž๋กœ ๋ฆฌํ„ด

SELECT
        EMP_NAME
    ,   HIRE_DATE
    ,   CEIL(MONTHS_BETWEEN(SYSDATE, HIRE_DATE)) >>> ์ง€๊ธˆ ํ˜„์žฌ์™€ ๊ณ ์šฉ๋‚ ์งœ์˜ ๊ฐœ์›” ์ˆ˜ ์ฐจ์ด ์ถœ๋ ฅ
   FROM EMPLOYEE;

3) ADD_MONTHS

ADD_MONTHS(๋‚ ์งœ, ์ˆซ์ž) : ๋‚ ์งœ์— ์ˆซ์ž๋งŒํผ ๊ฐœ์›” ์ˆ˜ ๋”ํ•ด์„œ ๋‚ ์งœ๋กœ ๋ฆฌํ„ด

SELECT
        ADD_MONTHS(SYSDATE, 5)	>>> ํ˜„์žฌ ๋‚ ์งœ์— 5๊ฐœ์›” ๋”ํ•œ ๊ฐ’ ์ถœ๋ ฅ
   FROM DUAL;

โ—ผ ๊ทผ๋ฌด๋…„์ˆ˜๊ฐ€ 20๋…„ ์ด์ƒ์ธ ์ง์›์˜ ๋ชจ๋“  ์ปฌ๋Ÿผ ์กฐํšŒ

SELECT
        *
   FROM EMPLOYEE
--WHERE MONTHS_BETWEEN(SYSDATE, HIRE_DATE) >= 240;
  WHERE ADD_MONTHS(HIRE_DATE, 240) <= SYSDATE;	>>> ์œ„ ์‹๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ

4) NEXT_DAY

NEXT_DAY(๊ธฐ์ค€๋‚ ์งœ, ์š”์ผ(๋ฌธ์ž or ์ˆซ์ž)) : ๊ธฐ์ค€ ๋‚ ์งœ์—์„œ ๊ตฌํ•˜๋ ค๋Š” ์š”์ผ์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋‚ ์งœ ๋ฆฌํ„ด

SELECT SYSDATE, NEXT_DAY(SYSDATE, '๊ธˆ์š”์ผ') FROM DUAL;
SELECT SYSDATE, NEXT_DAY(SYSDATE, '๊ธˆ') FROM DUAL;
SELECT SYSDATE, NEXT_DAY(SYSDATE, 6) FROM DUAL;	>>> ์ˆซ์ž๋Š” 1~7์ด๋ฉฐ ์ผ์š”์ผ๋ถ€ํ„ฐ ์‹œ์ž‘

SELECT SYSDATE, NEXT_DAY(SYSDATE, 'FRIDAY') FROM DUAL;	>>> ํ˜„์žฌ ์‹œ์Šคํ…œ ์„ค์ •์ด ํ•œ๊ธ€์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜๋ฌธ์€ X
-- ์‹œ์Šคํ…œ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์–ธ์–ด๊ฐ€ ์„ค์ •๋˜์–ด์žˆ์œผ๋ฏ€๋กœ ๋ณ€๊ฒฝ์„ ์›ํ•˜๋ฉด ์„ค์ •์„ ๋ณ€๊ฒฝํ•ด์„œ ์‚ฌ์šฉ
ALTER SESSION SET NLS_LANGUAGE = AMERICAN;	>>> ์‹œ์Šคํ…œ ์„ค์ •์„ ์˜๋ฌธ์œผ๋กœ ๋ฐ”๊ฟˆ
SELECT SYSDATE, NEXT_DAY(SYSDATE, 'FRIDAY') FROM DUAL;
SELECT SYSDATE, NEXT_DAY(SYSDATE, '๊ธˆ์š”์ผ') FROM DUAL;	>>> ํ˜„์žฌ ์‹œ์Šคํ…œ ์„ค์ •์ด ์˜๋ฌธ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๊ธ€์€ X
ALTER SESSION SET NLS_LANGUAGE = KOREAN; 	>>> ์‹œ์Šคํ…œ ์„ค์ •์„ ๋‹ค์‹œ ํ•œ๊ธ€๋กœ ๋ฐ”๊ฟˆ

5) LAST_DAY

LAST_DAY(๋‚ ์งœ) : ํ•ด๋‹น ์›”์˜ ๋งˆ์ง€๋ง‰ ๋‚ ์งœ๋ฅผ ๊ตฌํ•˜์—ฌ ๋ฆฌํ„ด

SELECT SYSDATE, LAST_DAY(SYSDATE) FROM DUAL;

โ—ผ ์‚ฌ์›๋ช…, ์ž…์‚ฌ์ผ, ์ž…์‚ฌํ•œ ์›”์˜ ๊ทผ๋ฌด์ผ์ˆ˜(์ฃผ๋ง ํฌํ•จ)

SELECT
        EMP_NAME
    ,   HIRE_DATE
    ,   LAST_DAY(HIRE_DATE) - HIRE_DATE + 1 "์ž…์‚ฌ์›”์˜ ๊ทผ๋ฌด์ผ์ˆ˜"
    								>>> + 1 : ์ผํ•œ ์ฒซ๋‚ ์„ ํฌํ•จํ•˜์—ฌ ์„ธ๊ธฐ ์œ„ํ•จ
   FROM EMPLOYEE;

6) EXTRACT

EXTRACT : ๋…„, ์›”, ์ผ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜์—ฌ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
EXTRACT(YEAR FROM ๋‚ ์งœ) : ๋…„๋„๋งŒ ์ถ”์ถœ
EXTRACT(MONTH FROM ๋‚ ์งœ) : ์›”๋งŒ ์ถ”์ถœ
EXTRACT(DAY FROM ๋‚ ์งœ) : ์ผ๋งŒ ์ถ”์ถœ

SELECT
        EXTRACT(YEAR FROM SYSDATE) ๋…„๋„
    ,   EXTRACT(MONTH FROM SYSDATE) ์›”
    ,   EXTRACT(DAY FROM SYSDATE) ์ผ
   FROM DUAL;

โ—ผ ์ง์›์˜ ์ด๋ฆ„, ์ž…์‚ฌ์ผ, ๊ทผ๋ฌด๋…„์ˆ˜ ์กฐํšŒ ('๊ทผ๋ฌด๋…„์ˆ˜'๋Š” 'ํ˜„์žฌ๋…„๋„ - ์ž…์‚ฌ๋…„๋„'๋กœ ์กฐํšŒ)

SELECT
        EMP_NAME
    ,   HIRE_DATE
    ,   EXTRACT(YEAR FROM SYSDATE) - EXTRACT(YEAR FROM HIRE_DATE)
   FROM EMPLOYEE;

โ—ผ ๊ทผ๋ฌด๋…„์ˆ˜๋ฅผ ๋งŒ์œผ๋กœ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์›”์˜ ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•ด์•ผํ•จ

SELECT
        EMP_NAME
    ,   HIRE_DATE
--  ,   MONTHS_BETWEEN(SYSDATE, HIRE_DATE) "๋งŒ ๊ทผ๋ฌด๋…„์ˆ˜" >>> ๊ฐœ์›” ์ˆ˜์˜ ์ฐจ์ด ๋ฐ˜ํ™˜
    ,   FLOOR(MONTHS_BETWEEN(SYSDATE, HIRE_DATE) / 12) "๋งŒ ๊ทผ๋ฌด๋…„์ˆ˜"
    						>>> ๊ฐœ์›” ์ˆ˜์˜ ์ฐจ๋ฅผ 12๋กœ ๋‚˜๋ˆ„์–ด ๋…„์œผ๋กœ ๋งŒ๋“ค๊ณ  ๋‚ด๋ฆผ์ฒ˜๋ฆฌ
   FROM EMPLOYEE;

๐Ÿ‘‰ ํ˜•๋ณ€ํ™˜ ํ•จ์ˆ˜

๐Ÿ“ ํ˜•๋ณ€ํ™˜ ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜

1) TO_CHAR
2) TO_DATE
3) TO_NUMBER

1) TO_CHAR

TO_CHAR(๋‚ ์งœ, ํฌ๋งท) : ๋‚ ์งœํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์žํ˜• ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝ
TO_CHAR(์ˆซ์ž, ํฌ๋งท) : ์ˆซ์žํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์žํ˜• ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝ

SELECT TO_CHAR(1234) FROM DUAL;             >>> 1234๊ฐ€ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๋€Œ์–ด์žˆ๋Š” ์ƒํƒœ
SELECT TO_CHAR(1234, '99999') FROM DUAL;    >>> ๋‹ค์„ฏ์ž๋ฆฌ์ด๋˜ ๊ณต๋ฐฑ1234 ์ถœ๋ ฅ
SELECT TO_CHAR(1234, '00000') FROM DUAL;    >>> ๋‹ค์„ฏ์ž๋ฆฌ์ด๋˜ 01234 ์ถœ๋ ฅ
SELECT TO_CHAR(1234, 'L99999') FROM DUAL;   >>> ์› ํ™”ํ ํ‘œ๊ธฐ
SELECT TO_CHAR(1234, '$99999') FROM DUAL;   >>> ๋‹ฌ๋Ÿฌ ํ™”ํ ํ‘œ๊ธฐ
SELECT TO_CHAR(1234, '00,000') FROM DUAL;   >>> 01,234 ์ถœ๋ ฅ
SELECT TO_CHAR(1234, '999') FROM DUAL;      >>> ํ‘œ๊ธฐ ๋ถˆ๊ฐ€

โ—ผ ์ง์› ํ…Œ์ด๋ธ”์—์„œ ์‚ฌ์›๋ช…, ๊ธ‰์—ฌ ์กฐํšŒ (๊ธ‰์—ฌ๋Š” '\9,000,000' ํ˜•์‹์œผ๋กœ ํ‘œ์‹œ)

SELECT
        EMP_NAME
    ,   TO_CHAR(SALARY, 'L9,999,999')   >>> ์ด์ฒ˜๋Ÿผ TO_CHAR๋Š” ํฌ๋งท ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ
    FROM EMPLOYEE;

โ—ผ ๋‚ ์งœ ๋ฐ์ดํ„ฐ ํฌ๋งท ์ ์šฉ ์‹œ์—๋„ TO_CHAR ํ•จ์ˆ˜ ์‚ฌ์šฉ

SELECT TO_CHAR(SYSDATE, 'PM HH24:MI:SS') FROM DUAL; 	>>> ์˜คํ›„ 1์‹œ = 13์‹œ๋กœ ํ‘œ๊ธฐ
SELECT TO_CHAR(SYSDATE, 'AM HH:MI:SS') FROM DUAL; 		>>> ์˜คํ›„ 1์‹œ = 1์‹œ๋กœ ํ‘œ๊ธฐ
SELECT TO_CHAR(SYSDATE, 'MON DY, YYYY') FROM DUAL;
SELECT TO_CHAR(SYSDATE, 'YYYY-fmMM-DD DAY') FROM DUAL;  >>> fm : 01์ด ์•„๋‹Œ 1๋กœ ํ‘œ๊ธฐ
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD DAY') FROM DUAL; 	>>> fm์ด ์—†์–ด 01๋กœ ํ‘œ๊ธฐ
SELECT TO_CHAR(SYSDATE, 'YEAR, Q') || '๋ถ„๊ธฐ' FROM DUAL;	>>> ex) TWENTY TWENTY-THREE, 1๋ถ„๊ธฐ 
SELECT
       EMP_NAME
     , TO_CHAR(HIRE_DATE, 'YYYY-MM-DD') ์ž…์‚ฌ์ผ
  FROM EMPLOYEE;
SELECT
       EMP_NAME
     , TO_CHAR(HIRE_DATE, 'YYYY"๋…„" MM"์›”" DD"์ผ"') ์ž…์‚ฌ์ผ
     						>>> ํ™‘๋”ฐ์˜ดํ‘œ ์•ˆ์— ์Œ๋”ฐ์˜ดํ‘œ๋กœ ๋ฌธ์ž ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ
  FROM EMPLOYEE;
SELECT
       EMP_NAME
     , TO_CHAR(HIRE_DATE, 'YYYY/MM/DD HH24:MI:SS') ์ƒ์„ธ์ž…์‚ฌ์ผ
  FROM EMPLOYEE;

โ—ผ ์˜ค๋Š˜ ๋‚ ์งœ์— ๋Œ€ํ•ด ๋…„๋„ 4์ž๋ฆฌ, ๋…„๋„ 2์ž๋ฆฌ,

SELECT
       TO_CHAR(SYSDATE, 'YYYY')
     , TO_CHAR(SYSDATE, 'RRRR')
     , TO_CHAR(SYSDATE, 'YY')
     , TO_CHAR(SYSDATE, 'RR')
     , TO_CHAR(SYSDATE, 'YEAR')
  FROM DUAL;

โ—ผ RR๊ณผ YY์˜ ์ฐจ์ด

RR์€ ๋‘์ž๋ฆฌ ๋…„๋„๋ฅผ ๋„ค์ž๋ฆฌ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ๋ฐ”๊ฟ€ ๋…„๋„๊ฐ€ 50๋…„ ๋ฏธ๋งŒ์ด๋ฉด 2000๋…„์„ ์ ์šฉํ•˜๊ณ  50๋…„ ์ด์ƒ์ด๋ฉด 1900๋…„์„ ์ ์šฉ.
YY๋Š” ๋…„๋„๋ฅผ ๋ฐ”๊ฟ€ ๋•Œ ๋ฌด์กฐ๊ฑด ํ˜„์žฌ ์„ธ๊ธฐ(2000๋…„)๋ฅผ ์ ์šฉ.

SELECT
       TO_CHAR(TO_DATE('980630', 'YYMMDD'), 'YYYY-MM-DD') >>> YY : 2098-06-30 ์ถœ๋ ฅ
  FROM DUAL; 
SELECT
       TO_CHAR(TO_DATE('980630', 'RRMMDD'), 'YYYY-MM-DD') >>> RR : 1998-06-30 ์ถœ๋ ฅ
  FROM DUAL; 

โ—ผ ์˜ค๋Š˜ ๋‚ ์งœ์—์„œ ์›”๋งŒ ์ถœ๋ ฅ

SELECT
       TO_CHAR(SYSDATE, 'MM')
     , TO_CHAR(SYSDATE, 'MONTH')
     , TO_CHAR(SYSDATE, 'MON')  >>> MONTH์™€ ๋™์ผํ•œ ์ถœ๋ ฅ
     , TO_CHAR(SYSDATE, 'RM')   >>> ๋กœ๋งˆ๋ฌธ์žํ‘œ๊ธฐ
  FROM DUAL;

โ—ผ ์˜ค๋Š˜ ๋‚ ์งœ์—์„œ ์ผ๋งŒ ์ถœ๋ ฅ

SELECT
       TO_CHAR(SYSDATE, '"1๋…„ ๊ธฐ์ค€ " DDD"์ผ ์งธ"')    >>> ๋…„
     , TO_CHAR(SYSDATE, '"๋‹ฌ ๊ธฐ์ค€ " DD"์ผ ์งธ"')      >>> ๋‹ฌ
     , TO_CHAR(SYSDATE, '"์ฃผ ๊ธฐ์ค€ " D"์ผ ์งธ"')       >>> ์ฃผ
  FROM DUAL;

โ—ผ ์˜ค๋Š˜ ๋‚ ์งœ์—์„œ ๋ถ„๊ธฐ์™€ ์š”์ผ ์ถœ๋ ฅ ์ฒ˜๋ฆฌ

SELECT
       TO_CHAR(SYSDATE, 'Q"๋ถ„๊ธฐ"')
     , TO_CHAR(SYSDATE, 'DAY')	>>> 'ํ† ์š”์ผ' ์ถœ๋ ฅ
     , TO_CHAR(SYSDATE, 'DY')	>>> 'ํ† ' ์ถœ๋ ฅ
  FROM DUAL;

โ—ผ EMPLOYEE ํ…Œ์ด๋ธ”์—์„œ ์ด๋ฆ„, ์ž…์‚ฌ์ผ ์กฐํšŒ( ์ž…์‚ฌ์ผ ํฌ๋งท์€ '2018๋…„ 6์›” 15์ผ (์ˆ˜)' ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅ)

SELECT
        EMP_NAME
    ,   TO_CHAR(HIRE_DATE, 'YYYY"๋…„" fmMM"์›”" DD"์ผ" "("DY")"')
    								>>> ์›”์— 0์„ ํ‘œ๊ธฐํ•˜์ง€์•Š๊ธฐ ์œ„ํ•ด fm ์ž…๋ ฅ
   FROM EMPLOYEE;

2) TO_DATE

TO_DATE(๋ฌธ์ž, ํฌ๋งท) : ๋ฌธ์žํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ ์งœํ˜• ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝ
TO_DATE(์ˆซ์ž, ํฌ๋งท) : ์ˆซ์žํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ ์งœํ˜• ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝ

SELECT
       TO_DATE('20100101', 'RRRRMMDD') >>> 10/01/01 ์ถœ๋ ฅ
  FROM DUAL;
SELECT
       TO_CHAR(TO_DATE('20100101', 'RRRRMMDD'), 'RRRR, MON') >>> ๋‚ ์งœ ๋ฐ์ดํ„ฐ๋กœ ๋ฐ”๊พผ ๋’ค, 
       														 >>> CHAR ํ˜•ํƒœ๋กœ ๋‹ค์‹œ ๋ณ€ํ™˜
                                                             >>> '2010, 1์›”' ์ถœ๋ ฅ
  FROM DUAL;
SELECT
       TO_DATE('041030 143000', 'RRMMDD HH24MISS')
  FROM DUAL;
SELECT
       TO_CHAR(TO_DATE('041030 143000', 'RRMMDD HH24MISS'), 'DD-MON-RRRR HH:MI:SS PM')
       													 >>> 30-10์›”-2004 02:30:00 ์˜คํ›„ ์ถœ๋ ฅ
  FROM DUAL;

โ—ผ EMPLOYEE ํ…Œ์ด๋ธ”์—์„œ 2000๋…„๋„ ์ดํ›„์— ์ž…์‚ฌํ•œ ์‚ฌ์›์˜ ์‚ฌ๋ฒˆ, ์ด๋ฆ„, ์ž…์‚ฌ์ผ์„ ์กฐํšŒ

SELECT
        EMP_NO
    ,   EMP_NAME
    ,   HIRE_DATE
   FROM EMPLOYEE
--WHERE HIRE_DATE >= TO_DATE('20000101', 'RRRRMMDD');
--WHERE HIRE_DATE >= '20000101';					>>> ์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ (๋ฌธ์ž์—ด์€ ๋‚ ์งœ๋กœ ์ž๋™ ํ˜•๋ณ€ํ™˜)
--WHERE HIRE_DATE >= TO_DATE(20000101, 'RRRRMMDD'); >>> ์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ
  WHERE HIRE_DATE >= 20000101;
  >>> ์ž๋™ ํ˜•๋ณ€ํ™˜์ด ๋˜์ง€์•Š์•„ '์ผ๊ด€์„ฑ ์—†๋Š” ๋ฐ์ดํ„ฐ ์œ ํ˜•: DATE์ด(๊ฐ€) ํ•„์š”ํ•˜์ง€๋งŒ NUMBER์ž„' ์˜ค๋ฅ˜
  >>> ์ˆซ์ž๋Š” ๋‚ ์งœ๋กœ ์ž๋™ ํ˜•๋ณ€ํ™˜ X

๐Ÿ“Œ Ref.

* ๋ฌธ์ž์—ด์€ '๋‚ ์งœ'๋กœ ์ž๋™ ํ˜•๋ณ€ํ™˜ O
* ์ˆซ์ž๋Š” '๋‚ ์งœ'๋กœ ์ž๋™ ํ˜•๋ณ€ํ™˜ X

3) TO_NUMBER

TO_NUMBER(๋ฌธ์ž, ํฌ๋งท) : ๋ฌธ์žํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์ˆซ์žํ˜• ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝ

SELECT TO_NUMBER('123456789') FROM DUAL;

โ—ผ ๋ฌธ์ž๊ฐ€ ์ˆซ์ž๋กœ ์ž๋™ํ˜•๋ณ€ํ™˜ ๋˜๋Š” ๊ฒฝ์šฐ

SELECT '123' + '456' FROM DUAL;	 >>> 123456์ด ์•„๋‹Œ 579 ์ถœ๋ ฅ (๊ฐ๊ฐ ์ž๋ฆฌ์ˆ˜๋ผ๋ฆฌ ๋”ํ•ด์ ธ ์—ฐ์‚ฐ๊ฒฐ๊ณผ ์ถœ๋ ฅ)
SELECT '123' + '456A' FROM DUAL; >>> '์ˆ˜์น˜๊ฐ€ ๋ถ€์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค' ์˜ค๋ฅ˜
								 >>> ์ˆซ์ž๋กœ ๋œ ๋ฌธ์ž์—ด๋งŒ ์ž๋™ ํ˜•๋ณ€ํ™˜ ๊ฐ€๋Šฅ
SELECT
       EMP_NAME
     , HIRE_DATE
  FROM EMPLOYEE
 WHERE HIRE_DATE = '90/02/06';	>>> ์ž๋™ ํ˜•๋ณ€ํ™˜

โ—ผ EMPLOYEE ํ…Œ์ด๋ธ”์—์„œ ์‚ฌ๋ฒˆ์ด ํ™€์ˆ˜์ธ ์ง์›๋“ค์˜ ์ •๋ณด ๋ชจ๋‘ ์กฐํšŒ

SELECT
       *
  FROM EMPLOYEE
 WHERE MOD(EMP_ID, 2) = 1; >>> EMP_ID๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ์ž๋™ ํ˜•๋ณ€ํ™˜ํ•˜์—ฌ ์—ฐ์‚ฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ

โ—ผ TO_NUMBER๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜•๋ณ€ํ™˜

SELECT '1,000,000' + '500,000' FROM DUAL;	>>> ์ž๋™ ํ˜•๋ณ€ํ™˜X (ํฌ๋งท๋œ ์ˆซ์ž์ด๊ธฐ ๋•Œ๋ฌธ์—) 
											>>> ์•„๋ž˜์˜ ์‹์ฒ˜๋Ÿผ TO_NUMBER ์‚ฌ์šฉํ•ด์•ผํ•จ
SELECT 
       TO_NUMBER('1,000,000', '99,999,999') + TO_NUMBER('500,000', '999,999')
  FROM DUAL;

โ—ผ ์ง์› ํ…Œ์ด๋ธ”์—์„œ ์‚ฌ์›๋ฒˆํ˜ธ๊ฐ€ 201์ธ ์‚ฌ์›์˜ ์ด๋ฆ„, ์ฃผ๋ฏผ๋ฒˆํ˜ธ ์•ž์ž๋ฆฌ, ์ฃผ๋ฏผ๋ฒˆํ˜ธ ๋’ท์ž๋ฆฌ, ์ฃผ๋ฏผ๋ฒˆํ˜ธ ์•ž์ž๋ฆฌ์™€ ๋’ท์ž๋ฆฌ์˜ ํ•ฉ์„ ์กฐํšŒ (๋‹จ, ์ž๋™ ํ˜•๋ณ€ํ™˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์กฐํšŒ)

SELECT
        EMP_NAME
    ,   EMP_NO
    ,   SUBSTR(EMP_NO, 1, 6) ์•ž์ž๋ฆฌ
    ,   SUBSTR(EMP_NO, 8) ๋’ท์ž๋ฆฌ
    ,   TO_NUMBER(SUBSTR(EMP_NO, 1, 6)) + TO_NUMBER(SUBSTR(EMP_NO, 8)) ๊ฒฐ๊ณผ
    	>>> TO_NUMBER๊ฐ€ ์—†์–ด๋„ ์ž๋™ํ˜•๋ณ€ํ™˜์ด ๋˜์–ด ์ถœ๋ ฅ๋ฌธ์€ ๊ฐ™์Œ
    FROM EMPLOYEE
    WHERE EMP_ID = TO_CHAR(201);
    >>> EMP_ID๊ฐ€ ๋ฌธ์ž์—ด์ด๋ฏ€๋กœ TO_CHAR ์‚ฌ์šฉ (but, ๊ธฐ์ž…ํ•˜์ง€์•Š์•„๋„ ์ž๋™ ํ˜•๋ณ€ํ™˜์ด ๋˜๊ธด ๋จ)

๐Ÿ“Œ Ref.

* ๋ช…์‹œํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ, ์ž๋™ ํ˜•๋ณ€ํ™˜์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋™์ž‘ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ํ•ญ์ƒ ์ธ์‹ํ•˜๊ณ ์žˆ์–ด์•ผํ•จ

๐Ÿ‘‰ NULL ์ฒ˜๋ฆฌ ํ•จ์ˆ˜

1) NVL

NVL(์ปฌ๋Ÿผ๋ช…, ์ปฌ๋Ÿผ๊ฐ’์ด NULL์ผ ๋•Œ ๋ฐ”๊ฟ€ ๊ฐ’)

SELECT
        EMP_NAME
    ,   BONUS
    ,   NVL(BONUS, 0) >>> NULL๊ฐ’์ด 0์œผ๋กœ ์น˜ํ™˜๋˜์–ด ์ถœ๋ ฅ
   FROM EMPLOYEE;

2) NVL2

NVL2(์ปฌ๋Ÿผ๋ช…, ๋ฐ”๊ฟ€ ๊ฐ’1, ๋ฐ”๊ฟ€ ๊ฐ’2)
: ํ•ด๋‹น ์ปฌ๋Ÿผ์ด ๊ฐ’์ด ์žˆ์œผ๋ฉด ๋ฐ”๊ฟ€ ๊ฐ’1๋กœ ๋ณ€๊ฒฝ, ํ•ด๋‹น ์ปฌ๋Ÿผ์ด NULL์ด๋ฉด ๋ฐ”๊ฟ€ ๊ฐ’2๋กœ ๋ณ€๊ฒฝ

โ—ผ ๋ณด๋„ˆ์Šค ํฌ์ธํŠธ๊ฐ€ NULL์ธ ์ง์›์€ 0.5๋กœ ๋ณด๋„ˆ์Šค ํฌ์ธํŠธ๊ฐ€ NULL์ด ์•„๋‹Œ ์ง์›์€ 0.7๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์กฐํšŒ

SELECT
        EMP_NAME
    ,   BONUS
    ,   NVL2(BONUS, 0.7, 0.5)
    FROM EMPLOYEE;

๐Ÿ‘‰ ์„ ํƒ ํ•จ์ˆ˜

1) DECODE

๐Ÿ’โ€ DECODE๋Š”,
์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ฒฝ์šฐ์— ์„ ํƒ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณต (์ผ์น˜ํ•˜๋Š” ๊ฐ’)
DECODE(๊ณ„์‚ฐ์‹ or ์ปฌ๋Ÿผ๋ช…, ์กฐ๊ฑด๊ฐ’1, ์„ ํƒ๊ฐ’1, ์กฐ๊ฑด๊ฐ’2, ์„ ํƒ๊ฐ’2 ...)

โ—ผ ์„ฑ๋ณ„์„ ๊ตฌ๋ถ„ํ•˜์—ฌ '๋‚จ' ๋˜๋Š” '์—ฌ'๋กœ ์กฐํšŒ

SELECT
        EMP_ID
    ,   EMP_NAME
    ,   EMP_NO
    ,   DECODE(SUBSTR(EMP_NO, 8, 1), '1', '๋‚จ', '2', '์—ฌ')
    FROM EMPLOYEE;

โ—ผ ๋งˆ์ง€๋ง‰ ์ธ์ž๋กœ ์กฐ๊ฑด๊ฐ’ ์—†์ด ์„ ํƒ๊ฐ’์„ ์ž‘์„ฑํ•˜๋ฉด ์•„๋ฌด๋Ÿฐ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜์ง€ ์•Š์„ ๋•Œ ๋งˆ์ง€๋ง‰์— ์ž‘์„ฑํ•œ ์„ ํƒ๊ฐ’์„ ๋ฌด์กฐ๊ฑด ์„ ํƒ (Java์˜ else์™€ ๋น„์Šท)

SELECT
        EMP_ID
    ,   EMP_NAME
    ,   EMP_NO
    ,   DECODE(SUBSTR(EMP_NO, 8, 1), '1', '๋‚จ', '์—ฌ')
    							>>> ์กฐ๊ฑด๊ฐ’2์˜€๋˜ '2'๋ฅผ ์ƒ๋žตํ•ด๋„ ์œ„์™€ ๋™์ผํ•œ ์ถœ๋ ฅ
                                >>> ์กฐ๊ฑด๊ฐ’ ์—†์ด ์„ ํƒ๊ฐ’์ธ '์—ฌ'๋งŒ ์žˆ์œผ๋ฏ€๋กœ ๋‚˜๋จธ์ง€๋Š” ๋ชจ๋‘ '์—ฌ'๋กœ ์ถœ๋ ฅ
    FROM EMPLOYEE;

โ—ผ ์ง์›์˜ ๊ธ‰์—ฌ๋ฅผ ์ธ์ƒํ•˜๊ณ ์ž ํ•จ. ์ง๊ธ‰ ์ฝ”๋“œ๊ฐ€ J7์ธ ์ง์›์€ ๊ธ‰์—ฌ์˜ 10%, J6์ธ ์ง์›์€ 15%, J5์ธ ์ง์›์€ 20%๋ฅผ ์ธ์ƒํ•˜๊ณ  ๊ทธ ์™ธ ์ง๊ธ‰์˜ ์ง์›์€ 5%๋งŒ ์ธ์ƒํ•œ๋‹ค. ์ง์›๋ช…, ์ง๊ธ‰์ฝ”๋“œ, ๊ธ‰์—ฌ, ์ธ์ƒ๊ธ‰์—ฌ(์œ„ ์กฐ๊ฑด) ์กฐํšŒ

SELECT
        EMP_NAME ์ง์›๋ช…
    ,   JOB_CODE ์ง๊ธ‰์ฝ”๋“œ
    ,   SALARY ๊ธ‰์—ฌ
    ,   DECODE(JOB_CODE, 'J7', SALARY * 1.1, 
                         'J6', SALARY * 1.15, 
                         'J5', SALARY * 1.2, 
                               SALARY * 1.05) ์ธ์ƒ๊ธ‰์—ฌ
   FROM EMPLOYEE;

2) CASE

๐Ÿ’โ€ CASE๋Š”,
์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ฒฝ์šฐ์— ์„ ํƒ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณต (๋ฒ”์œ„๊ฐ’๋„ ๊ฐ€๋Šฅ)

    CASE
        WHEN ์กฐ๊ฑด์‹ THEN ๊ฒฐ๊ณผ๊ฐ’
        WHEN ์กฐ๊ฑด์‹ THEN ๊ฒฐ๊ณผ๊ฐ’
        ELSE ๊ฒฐ๊ณผ๊ฐ’
    END

โ—ผ ๊ธ‰์—ฌ๊ฐ€ 500๋งŒ์›์„ ์ดˆ๊ณผํ•˜๋ฉด '์ž„์›', 300~500 ์‚ฌ์ด๋ฉด '๊ฒฝ๋ ฅ', ๊ทธ ์ดํ•˜๋Š” '์‹ ์ž…'์œผ๋กœ ์ถœ๋ ฅ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ณ„์นญ์€ '๊ตฌ๋ถ„'์œผ๋กœ ํ•จ

SELECT
        EMP_NAME
    ,   SALARY
    ,   CASE
            WHEN SALARY > 5000000 THEN '์ž„์›'
            WHEN SALARY BETWEEN 3000000 AND 5000000 THEN '๊ฒฝ๋ ฅ'
            ELSE '์‹ ์ž…'
        END ๊ตฌ๋ถ„
    FROM EMPLOYEE;

profile
Tiny little habits make me

0๊ฐœ์˜ ๋Œ“๊ธ€