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;