Oracle #7-2 - DQL ๋ฌธ์ž ์ฒ˜๋ฆฌ FUNCTION

ennakoidaยท2023๋…„ 6์›” 19์ผ
0

Oracle

๋ชฉ๋ก ๋ณด๊ธฐ
8/13

๐Ÿ“ ๋ฌธ์ž ์ฒ˜๋ฆฌ ํ•จ์ˆ˜

LENGTH, LENGTHB, INSTR(@์˜ ์œ„์น˜ ์•Œ๋ ค์คŒ), SUBSTR(๋ฌธ์ž ์ž๋ฆ„),
LPAD, RPAD(๊ธธ์ด ์ •ํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์ฑ„์›Œ์คŒ), LTRIM, RTRIM, TRIM(๊ณต๋ฐฑ์ œ๊ฑฐ) โ€ฆ

SUBSTR

-- ์‚ฌ์›๋ช…์—์„œ ์„ฑ๋งŒ ์ถœ๋ ฅํ•˜์„ธ์š”

SELECT SUBSTR(EMP_NAME, 1, 1) "์‚ฌ์› ์„ฑ" FROM EMPLOYEE;

SUBSTR๋Š” ์ปฌ๋Ÿผ์ด๋‚˜ ๋ฌธ์ž์—ด์—์„œ ์ง€์ •ํ•œ ์œ„์น˜๋ถ€ํ„ฐ ์ง€์ •ํ•œ ๊ฐœ์ˆ˜์˜ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ๋‚ด์–ด ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋กœ,

SUBSTR(STRING, POSITION(์‹œ์ž‘์ ), [LENGTH])์˜ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค.


DISTINCT

-- ์‚ฌ์›๋ช…์„ ์ค‘๋ณต์—†์ด ์ถœ๋ ฅํ•˜์„ธ์š”.

SELECT DISTINCT EMP_NAME "์‚ฌ์›๋ช…" FROM EMPLOYEE;

DISTINCT๋Š” ์ค‘๋ณต ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋กœ, ์ปฌ๋Ÿผ์— ํฌํ•จ๋œ ์ค‘๋ณต ๊ฐ’์„ ํ•œ๋ฒˆ์”ฉ๋งŒ ํ‘œ์‹œํ•˜๊ฒ ๋‹ค ๋ช…์‹œํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.


LPAD / RPAD

-- employee ํ…Œ์ด๋ธ”์—์„œ ๋‚จ์ž๋งŒ ์‚ฌ์›๋ช…, ์ฃผ๋ฏผ๋ฒˆํ˜ธ๋ฅผ ๋‚˜ํƒ€๋‚ด์„ธ์š”.
-- ์ฃผ๋ฏผ๋ฒˆํ˜ธ์˜ ๋’ท6์ž๋ฆฌ๋Š” *์ฒ˜๋ฆฌํ•˜์„ธ์š”.

SELECTEMP_NAME "์‚ฌ์›๋ช…", RPAD(SUBSTR(EMP_NO,1,8), 14, '*') "์ฃผ๋ฏผ๋ฒˆํ˜ธ" FROM EMPLOYEE WHERE SUBSTR(EMP_NO, 8, 1) IN (1, 3);

LPAD์™€ RPAD๋Š” ์ฃผ์–ด์ง„ ์ปฌ๋Ÿผ ๋ฌธ์ž์—ด์— ์ž„์˜์˜ ๋ฌธ์ž์—ด์„ ์™ผ์ชฝ(LPAD)/์˜ค๋ฅธ์ชฝ(RPAD)์— ๋ง๋ถ™์—ฌ ๊ธธ์ด N์˜ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

LPAD(STRING, N, [STR])์˜ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•˜๋ฉฐ, [STR]์€ ์ƒ๋žต ์‹œ, ๊ณต๋ฐฑ ๋ฌธ์ž๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.


LTRIM / RTRIM / TRIM

SELECT LTRIM('123HELLO', '123') FROM DUAL; -- ์™ผ์ชฝ๋ถ€ํ„ฐ ์‚ญ์ œ
SELECT RTRIM(LTRIM('123HELLO123', '123'), '123') FROM DUAL; -- ์™ผ์ชฝ > ์˜ค๋ฅธ์ชฝ
SELECT TRIM('Z' FROM 'ZZZZWORLDZZZ') FROM DUAL; -- TRIM์€ ์–‘์˜†์„ ์ง€์šธ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฌธ์ž ํ•˜๋‚˜์— ๋Œ€ํ•ด์„œ๋งŒ ์ง€์šธ ์ˆ˜ ์žˆ๋‹ค.

LTRIM๊ณผ RTRIM, TRIM์€ ์ฃผ์–ด์ง„ ์ปฌ๋Ÿผ์ด๋‚˜ ๋ฌธ์ž์—ด์˜ ์™ผ์ชฝ ํ˜น์€ ์˜ค๋ฅธ์ชฝ์—์„œ, ์ง€์ •ํ•œ STR์— ํฌํ•จ๋œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•œ ๋‚˜๋จธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

LTRIM(STRING, STR)์˜ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

๐Ÿ’ก

  • LRTIM : ์™ผ์ชฝ๋ถ€ํ„ฐ ์‚ญ์ œ
  • RTRIM : ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์‚ญ์ œ
  • TRIM : ์–‘์ชฝ ์ „๋ถ€ ์‚ญ์ œ. ๋‹จ, ํŠน์ •ํ•œ ๋ฌธ์ž ํ•˜๋‚˜์— ๋Œ€ํ•ด์„œ๋งŒ ์ง€์šธ ์ˆ˜ ์žˆ๋‹ค.

REPLACE

-- ์—ญ์‚ผ๋™ > ์‚ผ์„ฑ๋™

SELECT REPLACE ('์„œ์šธ์‹œ ๊ฐ•๋‚จ๊ตฌ ์—ญ์‚ผ๋™', '์—ญ์‚ผ๋™', '์‚ผ์„ฑ๋™') FROM DUAL;

REPLACE๋Š” ๋ฌธ์ž ํƒ€์ž… ์ปฌ๋Ÿผ ํ˜น์€ ๋ฌธ์ž์—ด ์ค‘, ํŠน์ • ๋ฌธ์ž(์—ด)๋ฅผ ๋‹ค๋ฅธ ๋ฌธ์ž(์—ด)๋กœ ์น˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

REPLACE(STRING, STR1, STR2)์˜ ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ, STR1์— ์ ์€ ๋ฌธ์ž์—ด์„ STR2์— ์ ์€ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

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