개발공부

[MySQL] 문자열의 일부분만 가져오기 substr(), substring() 본문

Database/MySQL

[MySQL] 문자열의 일부분만 가져오기 substr(), substring()

mscha 2022. 5. 16. 11:52

문자열의 일부분 가져오기

substr(), substring()

두개는 같은 함수이다.

substring(컬럼이름, 시작위치(sql은 1부터시작), 끝위치)

or

substr(컬럼이름, 시작위치(sql은 1부터시작), 끝위치)

 

아래와 같은 books 테이블이 있다.

1. title을 처음부터 10글자 까지만 가져오기

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

2. title의 맨 뒤에서 5번째 글자부터 끝가지 가져오기

select substring(title, -5) as title
from books;

3. title의 5번째 글자부터 12번째 글자까지 가져오기

select substr(title, 5, 12) as title
from books;