day2

์ƒ์€๐Ÿ‘ธยท2023๋…„ 11์›” 30์ผ
0

๋šœ๋ฒ…๋šœ๋ฒ… ์„ธ๋ฒˆ์งธ

๋ชฉ๋ก ๋ณด๊ธฐ
2/11
post-thumbnail

๐Ÿ“– SQL์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜ (๋‹จ์ผํ–‰ / ๋‹ค์ค‘๋ณต์ˆ˜ํ–‰)

(1) INITCAP(์ปฌ๋Ÿผ๋ช…) : ์ฒซ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ

SELECT INITCAP(ename) FROM emp;

๐Ÿ’ป

(2) LOWER(์ปฌ๋Ÿผ๋ช…) : ์†Œ๋ฌธ์ž๋กœ

SELECT LOWER(name), LOWER('ABCD') FROM student;

๐Ÿ’ป

(3) UPPER(์ปฌ๋Ÿผ๋ช…) : ๋Œ€๋ฌธ์ž๋กœ

SELECT UPPER(name) FROM student;

๐Ÿ’ป

(4) LENGTH(์ปฌ๋Ÿผ๋ช…) : ๋ฌธ์ž์—ด ๊ธธ์ด

LENGTHB(์ปฌ๋Ÿผ๋ช…) : ๋ฌธ์ž์—ด ๊ธธ์ด ๋ฐ”์ดํŠธ ๊ฐ’

SELECT ename, LENGTH(ename) FROM emp;

๐Ÿ’ป

(5) CONCAT(๋ฌธ์ž, ๋ฌธ์ž) : ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ '๋ฌธ์ž'||'๋ฌธ์ž' -> ๋ฌธ์ž 2๊ฐœ๋งŒ ์ด์–ด ๋ถ™์—ฌ์ฃผ๋Š” ์—ญํ• ! 3๊ฐœ๋Š” ์•ˆ๋ถ™์—ฌ์ง

SELECT CONCAT (CONCAT('ํ•˜๋‚˜', '๋‘˜'), '์…‹') FROM dual; --๋ฌธ์ž 2๊ฐœ๋งŒ ์ด์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— 3๊ฐœ ์ด์œผ๋ ค๋ฉด ์ค‘์ฒฉ์œผ๋กœ ์จ์ค˜์•ผ ํ•œ๋‹ค

๐Ÿ’ป

(6) SUBSTR() : ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž ์ถ”์ถœ

SELECT name, SUBSTR(name, 1, 1) FROM student; -- name 1๋ฒˆ์งธ์œ„์น˜๋ถ€ํ„ฐ 1๊ธ€์ž ์ถ”์ถœ

๐Ÿ’ป

(7) INSTR() : ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์˜ ์œ„์น˜ ์ฐพ๊ธฐ

INSTR(๋ฌธ์ž์—ด ๋˜๋Š” ์ปฌ๋Ÿผ๋ช…, ์ฐพ์„๊ธ€์ž, ์‹œ์ž‘์œ„์น˜, ๋ช‡๋ฒˆ์งธ)
INSTR(๋ฌธ์ž์—ด ๋˜๋Š” ์ปฌ๋Ÿผ๋ช…, ์ฐพ์„๊ธ€์ž)

SELECT **INSTR('I am Lunch', 'ch')** FROM dual; --ch์˜ ์œ„์น˜ : 9๋ฒˆ์งธ
SELECT **INSTR('I am Lunch, and menu is apple', 'a', 4)** FROM dual; --4๋ฒˆ์งธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋‚˜์˜ค๋Š” a์˜ ์œ„์น˜ : 13๋ฒˆ์งธ
SELECT **INSTR('I am Lunch, and menu is apple', 'a', 1, 3)** FROM dual; --1๋ฒˆ์งธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ 3๋ฒˆ์งธ์— ๋‚˜์˜ค๋Š” a์˜ ์œ„์น˜ : 25๋ฒˆ์งธ

๐Ÿ’ป

๐Ÿ’ป

๐Ÿ’ป

SELECT name, INSTR(name, ' ') FROM student;
--name์—์„œ ๋„์–ด์“ฐ๊ธฐ ๋‚˜์˜ฌ๋•Œ๊นŒ์ง€์˜ ์œ„์น˜!

๐Ÿ’ป

(8) LPAD() : ์™ผ์ชฝ์„ ๋ฌธ์ž๋กœ ์ฑ„์›€ LPAD(์ปฌ๋Ÿผ, ์ž๋ฆฟ์ˆ˜, ์ฑ„์šธ๋ฌธ์ž)

RPAD() : ์˜ค๋ฅธ์ชฝ์„ ๋ฌธ์ž๋กœ ์ฑ„์›€

SELECT LPAD('3000', 10, '-') FROM dual; --10์ž๋ฆฌ๊ฐ€ ๋ ๋•Œ๊นŒ์ง€ ์™ผ์ชฝ์„ -๋กœ ์ฑ„์›€

๐Ÿ’ป

(9) LTRIM() : ์™ผ์ชฝ์— ๋ฌธ์ž๋ฅผ ์‚ญ์ œ LTRIM(์ปฌ๋Ÿผ, ์ง€์šธ๋ฌธ์ž)

RTRIM() : ์˜ค๋ฅธ์ชฝ์— ๋ฌธ์ž๋ฅผ ์‚ญ์ œ

SELECT job, LTRIM(job, 'M') FROM emp;

๐Ÿ’ป

(10) TRIM() : ์–‘์ชฝ์— ๊ณต๋ฐฑ์ œ๊ฑฐ --์•„์ด๋”” abc123 ๋กœ๊ทธ์ธ=> ๋„์–ด์“ฐ๊ธฐ๊นŒ์ง€ ๊ฐ™์ด ๋„˜์–ด๊ฐˆ์ˆ˜๋„ ์žˆ์Œ! ๊ทธ๋ž˜์„œ ๊ณต๋ฐฑ์ œ๊ฑฐํ•˜๊ณ  ๋‚œ ๋‹ค์Œ์— ๋„˜๊ธด๋‹ค

(11) REPLACE() : ๋ฌธ์ž๋ฅผ ๋ฐ”๊ฟˆ REPLACE(๋ฌธ์ž์—ด ๋˜๋Š” ์ปฌ๋Ÿผ๋ช…, ์˜ฌ๋“œ, ๋‰ด)

โœ๏ธ์—ฐ์Šต๋ฌธ์ œ

--Student ํ…Œ์ด๋ธ”์—์„œ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 1 ์ „๊ณต์ด 101 ๋ฒˆ์ธ ํ•™์ƒ๋“ค์˜ ์ด๋ฆ„๊ณผ ์ „ํ™”๋ฒˆํ˜ธ,
--์ „ํ™”๋ฒˆํ˜ธ์—์„œ ๊ตญ๋ฒˆ ๋ถ€๋ถ„๋งŒ โ€˜*โ€™ ์ฒ˜๋ฆฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜์„ธ์š”.

SELECT 
	name,
    tel,
    **REPLACE(tel, SUBSTR(tel, 5, 3), '***')** REPLACE
FROM student
WHERE deptno1 = 101;

๐Ÿ’ป

๐Ÿšจ 02)6788-4861 ์€ 5๋ฒˆ์งธ ์ž๋ฆฌ์ธ 7๋ถ€ํ„ฐ 3์ž๋ฆฌ 788๋งŒ ๊ฐ€๋ ค์ง€๊ฒŒ ๋œ๋‹ค

SELECT 
	name,
    tel,
    REPLACE(tel, SUBSTR(tel, INSTR(tel, ')')+1,  INSTR(tel, '-') - ( INSTR(tel, ')')+1 ) ), '***')
    
FROM student
WHERE deptno1 = 101;

=> SUBSTR(tel, INSTR(tel, ')')+1 ์ž๋ฆฌ ๋ถ€ํ„ฐ INSTR(tel, '-') - ( INSTR(tel, ')')+1 ) ๊นŒ์ง€!!

=> SUBSTR() ์ž๋ฆฌ๋ฅผ ***๋กœ ๋ฐ”๊พธ๊ฒ ๋‹ค!

๐Ÿ’ป

SELECT 
	name,
    tel,
    SUBSTR(tel, 1, INSTR(tel, ')'))  --์•ž
            || RPAD('*', INSTR(tel, '-') - ( INSTR(tel, ')')+1 ), '*') --์ค‘๊ฐ„ RPAD(์ปฌ๋Ÿผ, ์ž๋ฆฟ์ˆ˜, ์ฑ„์šธ๋ฌธ์ž)
            || SUBSTR(tel, INSTR(tel, '-') , LENGTH(tel)) REPLACE3 --๋’ค
FROM student
WHERE deptno1 = 101;

๐Ÿ’ป

profile
๋’ค์ฃฝ๋ฐ•์ฃฝ ๋ฒจ๋กœ๊ทธ

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