코딩일기

[Oracle DB] 단일행 함수 - 문자 함수

-Becca- 2022. 5. 10. 20:48

[문자 함수]

 ⬆ UPPER 

•  문자열 → 모두 대문자로 변경

select upper('Hello World')
from dual;
upper('Hello World')
HELLO WORLD

 ⬇️ LOWER 

•  문자열 → 모두 소문자로 변경

select lower('Hello World')
from dual;
lower('Hello World')
hello world

 ↗️ INITCAP 

•  문자열 → 첫 문자만 대문자로 변경 (띄어쓰기 다음 문자를 대문자로)

select initcap('hello world')
from dual;
initcap('hello world')
Hello World

•  ex. 사번이 홀수인 사람 검색

select empno, ename
from emp
where mod(empno, 2) = 1
empno ename
7839 SMITH
7499 ALLEN
... ...

 📏 LENGTH / LENGTHB 

LENGTH

•  문자열의 길이 출력 (공백 포함)

select length('환영합니다!')
from dual;
length('환영합니다!')
6

LENGTHB

•  문자열의 길이가 몇 byte인지 출력

select lengthb('환영합니다!') -- 한글 : 2byte
from dual;
lengthb('환영합니다!')
11

 🏷 SUBSTR / SUBSTRB 

SUBSTR

•  문자열 내에서 부분 추출

select substr('Hello World', 2, 3) -- 2번 자리에서 3자리
from dual;
1 2 3 4 5 6 7 8 9 10 11
H e l l o   W o r l d
substr('Hello World', 2, 3)
ell

•  두 번째 인자 음수  뒤쪽부터

select substr('Hello World', -5, 3) -- (-5, 6) : 끝까지 나옴 (오류 X)
from dual;
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
H e l l o   W o r l d
substr('Hello World', -5, 3)
Wor

SUBSTRB

•  byte 수 만큼 출력

select substr('웰컴투오라클', 3, 4), substrb('웰컴투오라클', 3, 4)
from dual;
1, 2 3, 4 5, 6 7, 8 9, 10 11, 12
substr('웰컴투오라클', 3, 4) substrb('웰컴투오라클', 3, 4)
투오라클 컴투
select substrb('웰컴투오라클', 4, 4) -- (공백)투(공백)
from dual
substrb('웰컴투오라클', 4, 4)
 투 

 🔖 INSTR / INSTRB 

INSTR

•  문자열에서 지정된 위치에 존재하는 문자 위치

select instr('Hello World', 'W')
from dual
1 2 3 4 5 6 7 8 9 10 11
H e l l o   W o r l d
instr('Hello World', 'W')
7
select instr('Hello World', 'o')
from dual
1 2 3 4 5 6 7 8 9 10 11
H e l l o   W o r l d
instr('Hello World', 'o')
5
select instr('Hello World', 'o', 5, 2)
from dual
1 2 3 4 5 6 7 8 9 10 11
H e l l o   W o r l d
instr('Hello World', 'o', 5, 2)
8

•  두 번째 인자 (5) : 5번째 문자부터 시작

•  첫 번째 인자 ('o') : 문자 'o'를 검색

•  두 번째 인자 (2) : 2번째 발견되는 'o'의 위치

select instr('Hello World', 'l', -7, 2)
from dual
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
H e l l o   W o r l d
instr('Hello World', 'l', -7, 2)
3

•  두 번째 인자(-7) : 뒤에서 7번째 문자부터 시작

•  첫 번째 인자('l') : 문자 'l'을 검색

•  세 번째 인자(2) : 2번째 발견되는 'l'의 위치 (양수)

 

INSTRB

•  byte 수 기준으로 문자 위치 찾음

select instr('데이터베이스', '이', 3, 1), instrb('데이터베이스', '이', 3, 1)
from dual;
1, 2 3, 4 5, 6 7, 8 9, 10 11, 12
instr('데이터베이스', '이', 3, 1) instrb('데이터베이스', '이', 3, 1)
5 3

 🪄 LPAD / RPAD 

LPAD

•  오른쪽 정렬 후, 특정 문자를 왼쪽에 채움

select lpad('Oracle 10g', 20, '#')
from dual;
lpad('Oracle 10g', 20, '#')
##########Oracle 10g
select lpad('Oracle 10g', 21, 'xy')
from dual;
lpad('Oracle 10g', 21, 'xy')
xyxyxyxyxyxOracle 10g

RPAD

•  왼쪽 정렬 후, 특정 문자를 오른쪽에 채움

select rpad('Oracle 10g', 20, '#')
from dual;
rpad('Oracle 10g', 20, '#')
Oracle 10g##########

 ✂️ LTRIM / RTRIM / TRIM 

LTRIM

•  제거 문자 지정 X → 왼쪽 공백 제거

select ltrim('   Oracle 10g   ')
from dual;
ltrim('   Oracle 10g   ')
Oracle 10g   

•  제거 문자 지정 → 왼쪽(앞)의 특정 문자 제거 (대소문자 구분)

select ltrim('aaaOracle 10gaaa', 'a')
from dual;
ltrim('aaaOracle 10gaaa', 'a')
Oracle 10gaaa

•  제거 문자 2자 이상

select ltrim('xxyxXxyThis wordyxXyxyy', 'xy') -- x or y
from dual
ltrim('xxyxXxyThis wordyxXyxyy', 'xy')
XxyThis wordyxXyxyy

RTRIM

•  제거 문자 지정 X → 오른 공백 제거

select rtrim('   Oracle 10g   ')
from dual;
rtrim('   Oracle 10g   ')
   Oracle 10g

•  제거 문자 지정 → 오른쪽(앞)의 특정 문자 제거 (대소문자 구분)

select rtrim('aaaOracle 10gaaa', 'a')
from dual;
rtrim('aaaOracle 10gaaa', 'a')
aaaOracle 10g

•  제거 문자 2자 이상

select rtrim('xxyxXxyThis wordyxXyxyy', 'xy') -- x or y
from dual
rtrim('xxyxXxyThis wordyxXyxyy', 'xy')
xxyxXxyThis wordyxX

TRIM

•  앞, 뒤 특정 문자 삭제

select trim('a' from 'aaaOracle 10gaaa')
from dual;
trim('a' from 'aaaOracle 10gaaa')
Oracle 10g

 🔩 CONCAT 

•  두 개의 문자 결합

select concat(concat(ename, ' is a '), job) -- ename || ' is a ' || job
from dual;
concat(concat(ename, ' is a '), job)
SMITH is a CLERK
ALLEN is a SALESMAN
...

 🔄 REPLACE 

•  문자에서 해당 문자 지정된 문자 변경

select replace('JACK and JUE', 'J', 'BL') -- J를 BL로 변경
from dual;
replace('JACK and JUE', 'J', 'BL')
BLACK and BLUE