본문 바로가기

DB/오라클

[ORACLE]문자함수

 

UPPER대문자

LOWER소문자

INITCAP첫글자 대문자,나머지 소문자

 

select eno,ename,dno
from employee
where lower(ename)='scott'; --비교대상인 사원이름을 모두 소문자로 변환하여 비교
--where ename=upper('scott';)--값을 대문자로 변환하여 비교 
--where initcap(ename)='Scott';

 

LENGTH 문자길이

select length('Apple'), length('사과') from dual;--5 ,2

 

REPLACE(컬럼명,'찾을문자','변환문자') :찾을 문자를 변경하거나 제거

--(예)찾을 문자를 변경
update test
set address=replace(address,'광역시','시')
where address like '%광역시';

--(예)찾을 문자를 제거
update test
set address=replace(address,'광역','')
where address like '%광역시';

 

CONCAT 두개의 문자를 하나로 결합

 

concat('Apple','사과') as "함수 사용", 
'Apple' || '사과'|| '맛있어' as "||사용" --3개이상의 문자결합
from dual;

SUBSTR

(기존문자열, 시작index, 추출할 개수) 
시작index 음수-문자열의 마지막을 기준으로 거슬러 올라감

select *
from employee
where substr(ename,-1,1)='N';

[문제]87년도에 입사한 사원 정보 검색
select *
from employee
where substr(hiredate,1,2) = '87';--오라클: 날짜 기본 형식 'yy/mm/dd'
--where substr(hiredate,1,4)='1987'; --결과 없음


[문제]'급여가 50으로 끝나는'사원의 사원이름과 급여 출력
--[방법1]
select ename,salary
from employee
where salary like '%50'; -- salary는 실수 number(7,2)타입이지만 '문자로 자동형변환'되어 비교

--[방법2]
select ename,salary
from employee
--where substr(salary,-2,2) = '50';--salary는 실수 number(7,2)타입이지만 '문자로 자동형변환'
where substr(salary,-2,2)= 50 --'50'=50   50=50 문자 '50'이 수50으로 자동형변환 되어 비교됨


--[방법3] : 자동 형변환되어 비교된다는 사실을 모른다면
select ename,salary
from employee
where sbustr(TO_CHAR(salary),-2,2)='50';--TO_CHAR (수나 날짜)를 문자로 형변환해야함

 INSTR

(대상문자열, 찾을 문자열, 시작 index, 몇 번째 발견(1,1이면 생략가능)

해당문자열이 어느위치에 있는지 index 값을 반환시킴

 

select instr('apple','p',2,2)--3
from dual;

select instr('apple','p',3,1)--3
from dual;

select instr('apple','p',3,2)--0(※자바에서는 -1)
from dual;


select instr('apple','pl',1,1)--3
from dual;

--이름의 세번째 글자가 'R'인 사원의 정보 검색
select *
from employee
where ename like '__R%';

select *
from employee
where substr(ename,3,1)='R';


select *
from employee
where instr(ename,'R',3,1)=3;

LPAD '컬럼'이나 대상문자열을 명시된 자릿수에서 오른쪽에 나타내고 남은 왼쪽은 특정 기호로 채운다
RPAD '컬럼'이나 대상문자열을 명시된 자릿수에서 왼쪽에 나타내고 남은 오른쪽은 특정 기호로 채운다

select salary, LPAD(salary,10,'*')--왼쪽 자리를 *로 맞추고 salary 데이터를 오른쪽으로 정렬
from employee;

select salary, LPAD(salary,10,' ')--빈 공백으로 채움
from employee;

select salary, RPAD(salary,10,'*')----10자리를 마련 후 salary는 왼쪽, 남은 오른쪽자리를 '*'로 채움
from employee;

LTRIM문자열의 왼쪽 공백제거
RTRIM문자열의 오른쪽 공백제거
TRIM문자열의 양쪽 공백 제거

 

TRIM('특정문자1개만'  From  컬럼이나 '대상문자열') 특정문자가 첫번째 글자나 마지막글자면 잘라내고 남은문자열 반환

select TRIM('사과'from'사과매니아')
from dual;--*오류 메시지: trim set should have only one character*/

select TRIM('사'from'사과매니아')
from dual;--'과매니아'

select TRIM('아'from'사과매니아')
from dual;--'사과매니'

select TRIM('과'from'사과매니아')
from dual;--'사과매니아':'과'가 처음이나 마지막 글자가 아니므로 잘라내지못해 '사과매니아'그대로 리턴

'DB > 오라클' 카테고리의 다른 글

[ORACLE]그룹함수 count  (0) 2022.06.30
[ORCLE]일반함수 NVL,DECODE,CASE-END....  (0) 2022.06.24
[ORACLE]형변환 함수 -To_char,date,number  (0) 2022.06.21
[ORACLE]숫자함수, 날짜함수  (0) 2022.06.14
[ORACLE]조건문,정렬  (0) 2022.06.07