SUBSTR 함수는 문자열 string 내의 position 위치로부터 substring_length 길이의 문자열을 추출하여 반환한다. 만약, position 값으로 음수가 지정되면, 문자열의 끝에서부터 역방향으로 위치를 산정한다. 또한, substring_length가 생략되는 경우, 주어진 position 위치로부터 마지막까지 문자열을 추출하여 반환한다.
문자 단위가 아닌 바이트 단위로 시작 위치와 문자열의 길이를 산정한다는 점을 주의한다. 멀티바이트 문자 세트에서는 한 문자를 표현하는 바이트 수를 고려하여 인자를 지정해야 한다.
SUBSTR( string, position [, substring_length])
string :
• character string
• bit string
• NULL
position :
• integer
• NULL
substring_length :
• integer
--character set is euc-kr for Korean characters
--it returns empty string when substring_length is 0
SELECT SUBSTR('12345abcdeabcde',6, 0);
======================
''
--it returns 4-length substrings counting from the position
SELECT SUBSTR('12345abcdeabcde', 6, 4), SUBSTR('12345abcdeabcde', -6, 4);
============================================
'abcd' 'eabc'
--it returns substrings counting from the position to the end
SELECT SUBSTR('12345abcdeabcde', 6), SUBSTR('12345abcdeabcde', -6);
============================================
'abcdeabcde' 'eabcde'
-- it returns 4-length substrings counting from 16th position on double byte charset
SELECT SUBSTR ('12345가나다라마가나다라마', 16 , 4);
======================
'가나'