๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[DB] ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค MySQL - SQL์˜ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋“ค(concat(), concat_ws(), substring(), replace(), reverse(), char_length(), upper(), lower()) ๋ณธ๋ฌธ

๐Ÿ›ข๏ธDB/MySQL

[DB] ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค MySQL - SQL์˜ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋“ค(concat(), concat_ws(), substring(), replace(), reverse(), char_length(), upper(), lower())

๋ฃจ๋ฆฌ์•ผใ…‘ 2023. 11. 29. 18:47
๋ฐ˜์‘ํ˜•

MySQL์—์„œ ์ œ๊ณตํ•˜๋Š” ์ฃผ์š” ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋“ค์€ ๋‹ค์–‘ํ•œ ์ž‘์—…์— ์‚ฌ์šฉ๋œ๋‹ค. 

ํ•จ์ˆ˜ ์„ค๋ช… ์˜ˆ์‹œ
CONCAT() ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•œ๋‹ค. CONCAT('Hello', ' ', 'World') -> 'Hello World'
SUBSTRING() ๋ฌธ์ž์—ด์—์„œ ์ผ๋ถ€๋ถ„์„ ์ถ”์ถœํ•œ๋‹ค. SUBSTRING('MySQL', 1, 3) -> 'My'
LEFT() ๋ฌธ์ž์—ด์˜ ์™ผ์ชฝ์—์„œ ์ง€์ •๋œ ์ˆ˜์˜ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. LEFT('MySQL', 3) -> 'MyS'
RIGHT() ๋ฌธ์ž์—ด์˜ ์˜ค๋ฅธ์ชฝ์—์„œ ์ง€์ •๋œ ์ˆ˜์˜ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ ํ•œ๋‹ค. RIGHT('MySQL', 3) -> 'SQL'
LENGTH() / CHAR_LENGTH() ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ ํ•œ๋‹ค. LENGTH('Hello') -> 5
UPPER() ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ ํ•œ๋‹ค. UPPER('hello') -> 'HELLO'
LOWER() ๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ ํ•œ๋‹ค. LOWER('WORLD') -> 'world'
REPLACE() ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ถ€๋ถ„์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒด ํ•œ๋‹ค. REPLACE('Hello, World', 'Hello', 'Hi') -> 'Hi, World'
TRIM() ๋ฌธ์ž์—ด์˜ ์–‘ ๋์— ์žˆ๋Š” ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•œ๋‹ค. TRIM(' Hello ') -> 'Hello'
LTRIM() ๋ฌธ์ž์—ด์˜ ์™ผ์ชฝ์— ์žˆ๋Š” ๊ณต๋ฐฑ์„ ์ œ๊ฑฐ ํ•œ๋‹ค. LTRIM(' Hello ') -> 'Hello '
RTRIM() ๋ฌธ์ž์—ด์˜ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ๊ณต๋ฐฑ์„ ์ œ๊ฑฐ ํ•œ๋‹ค. RTRIM(' Hello ') -> ' Hello'
LOCATE() / INSTR() ๋ฌธ์ž์—ด์—์„œ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค. LOCATE('l', 'Hello') -> 3
CONCAT_WS() ์ง€์ •๋œ ๊ตฌ๋ถ„์ž๋กœ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•œ๋‹ค. CONCAT_WS('-', '2023', '11', '29') -> '2023-11-29'

 

์•„๋ž˜์˜ ๋ฐ์ดํ„ฐ๋กœ ์˜ˆ์ œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์ž.

books ํ…Œ์ด๋ธ”

 

concat() 

๋ฌธ์ž์—ด์„ ํ•ฉ์น˜๋Š” ํ•จ์ˆ˜

author_fname ๊ณผ author_lname ์ปฌ๋Ÿผ์˜ ๋ฌธ์ž์—ด์„
ํ•˜๋‚˜๋กœ ํ•ฉํ•ด์„œ, full_name์œผ๋กœ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ๋‹ค.

select concat(author_fname,' ', author_lname) as full_name   -- as full_name์€ ์ปฌ๋Ÿผ๋ช… ๋ณ€๊ฒฝ
from books;

 

concat_ws()

์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€, ๋ฌธ์ž์—ด์„ ๋ถ™์ผ ๋•Œ ์‚ฌ์šฉํ•  ๋ฌธ์ž์—ด์ด๋‹ค.

select concat_ws(' ', author_fname, author_lname) as full_name
from books;

์ด๋ฆ„ ๋‘๊ฐœ๋ฅผ ๋ถ™์—ฌ์„œ, full_name์„ ๋งŒ๋“ค๊ณ ,
์ฑ… ์ œ๋ชฉ๊ณผ ์ถœ๊ฐ„๋…„๋„๋„ ๊ฐ™์ด ๋ณด์—ฌ๋‹ฌ๋ผ.

select concat_ws(' ', author_fname, author_lname) as full_name, title, released_year
from books;

substring()

๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ถ„๋งŒ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜

substring ํ•จ์ˆ˜์˜ ์‹œ์ž‘์œ„์น˜๋Š” 1๋ถ€ํ„ฐ โ˜…โ˜…
(๋‹ค๋ฅธ ์–ธ์–ด๋ž‘ ๋‹ค๋ฅด๊ฒŒ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.. )

์ฑ… ์ œ๋ชฉ์„, ์ฒซ ๊ธ€์ž๋ถ€ํ„ฐ 10๋ฒˆ์งธ ๊ธ€์ž๊นŒ์ง€๋งŒ ๊ฐ€์ ธ์˜ค์‹œ์˜ค.

-- substring(์ปฌ๋Ÿผ๋ช…, ์‹œ์ž‘์œ„์น˜, ๋ ์œ„์น˜) as title ์ปฌ๋Ÿผ๋ช…๋„ ๋ณ€๊ฒฝ 
select substring(title, 1,  10) as title
from books;

์ œ๋ชฉ์„, ๋งจ ๋’ค์—์„œ 5๋ฒˆ ์งธ ๊ธ€์ž๋ถ€ํ„ฐ, ๋๊นŒ์ง€ ๋‚˜์˜ค๋„๋ก ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค์‹œ์˜ค.

select substring(title, -5)
from books;

์ œ๋ชฉ์„, ์•ž์—์„œ 3๋ฒˆ ์งธ ๊ธ€์ž๋ถ€ํ„ฐ, ๋๊นŒ์ง€ ๋‚˜์˜ค๋„๋ก ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค์‹œ์˜ค.

select substring(title, 3)
from books;

replace()

๋ฌธ์ž์—ด์˜ ๋‚ด์šฉ์„ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜

์ฑ… ์ œ๋ชฉ์—, The๊ฐ€ ์žˆ์œผ๋ฉด, Hello๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ์„ ๋•Œ

select replace(title, 'The', 'Hello')
from books;

๋ณ€๊ฒฝ ์ „ ํ›„

์ฑ… ์ œ๋ชฉ์—, The๊ฐ€ ์žˆ์œผ๋ฉด, ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ์„ ๋•Œ

select replace(title, 'The', '')
from books;

๋ณ€๊ฒฝ ์ „ ํ›„

 

reverse()

๋ฌธ์ž์—ด์˜ ์ˆœ์„œ๋ฅผ, ์—ญ์ˆœ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ํ•จ์ˆ˜

์ž‘๊ฐ€ author_lname ์„ ์—ญ์ˆœ์œผ๋กœ ๊ฐ€์ ธ์˜ค์‹œ์˜ค.

select reverse(author_lname) as author 
from books;

 

char_length()

๋ฌธ์ž์—ด์˜ ๊ฐฏ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜

์ฑ… ์ œ๋ชฉ์˜ ๊ธ€์ž ๊ฐฏ์ˆ˜๋ฅผ ๊ตฌํ•˜์„ธ์š”.

select char_length(title) as length, title, pages
from books;

 

upper() lower()

๋Œ€์†Œ๋ฌธ์ž ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜

author_fname์€ ๋Œ€๋ฌธ์ž๋กœ, author_lanme์€ ์†Œ๋ฌธ์ž๋กœ ๊ฐ€์ ธ์˜ค์‹œ์˜ค.

select upper(author_fname) as author_fname, lower(author_lname) as author_lname
from books;

์—ฌ๋Ÿฌ ํ•จ์ˆ˜์˜ ์กฐํ•ฉํ•˜๊ธฐ

์ฑ… ์ œ๋ชฉ์„ ๋งจ ์•ž๋ถ€ํ„ฐ 10๊ธ€์ž๊นŒ์ง€๋งŒ ๊ฐ€์ ธ์˜ค๊ณ , ๋’ค์— ...์„ ๋ถ™์ธ๋‹ค. 
(๋จผ์ € ์„œ๋ธŒ์ŠคํŠธ๋ง ์“ฐ๊ณ  ๊ทธ ๋‹ค์Œ ํ•จ์ˆ˜ ์”Œ์šฐ๊ธฐ)

select concat(substring(title, 1, 10),'...') as title
from books;

๋ฐ˜์‘ํ˜•