MySQL

MySQL) Substring, Replace, Upper/Lower ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ

567Rabbit 2024. 5. 14. 14:42

substring

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

 

-- ์ฑ… ์ œ๋ชฉ์„ ์ฒซ๊ธ€์ž๋ถ€ํ„ฐ ์—ด๋ฒˆ์งธ ๊ธ€์ž๊นŒ์ง€๋งŒ ๊ฐ€์ ธ์˜ค์‹œ์˜ค
-- substring ํ•จ์ˆ˜์˜ ์‹œ์ž‘ ์œ„์น˜๋Š” 1๋ถ€ํ„ฐ๋‹ค
select substring(title,1,10) as title, pages, released_year
from books;


-- ์ œ๋ชฉ์„, ๋งจ ๋’ค์—์„œ 5๋ฒˆ์งธ ๊ธ€์ž๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋‹ค ๋‚˜์˜ค๋„๋ก ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค์‹œ์˜ค
select substring(title,-5) as title
from books;

-- ์ œ๋ชฉ์„ ์•ž์—์„œ 3๋ฒˆ์งธ ๊ธ€์ž๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋‹ค ๋‚˜์˜ค๋„๋ก ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค์‹œ์˜ค
select substring(title,3) as title
from books;


-- ์ฑ… ์ œ๋ชฉ์„, ๋งจ ์•ž๋ถ€ํ„ฐ 10๊นŒ์ง€๋งŒ ๊ฐ€์ ธ์˜ค๊ณ , ๋’ค์—๋Š” ...์„ ๋ถ™์—ฌ์ฃผ์„ธ์š”
-- The Namesa...
select concat(substring(title,1,10),'...') as title
from books ;

 

 

 

 

replace

 

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

 

-- ์ฑ… ์ œ๋ชฉ์—, The๊ฐ€ ์žˆ์œผ๋ฉด, Hello๋กœ ๋ฐ”๊พธ๊ณ ์‹ถ๋‹ค.

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


select replace(title,' ','->')
from books;

 

 

 

 

upper/lower

 

-- ๋Œ€๋ฌธ์ž / ์†Œ๋ฌธ์ž ๋ณ€ํ™˜ํ•จ์ˆ˜ upper() / lower()

select replace(lower(title),'the','Hello') as title
from books;

select replace(upper(title),'the','Hello') as title
from books;