스터디/DB(ORACLE) ORACLE 문자열 함수 고무곰(GomuGom) 2017. 9. 4. 11:19 --[ 문자형 함수 ] --문자열과 문자열을 연결하는 함수 : CONCAT(char1,char2) or || 를 이용 select CONCAT('HELLO','_World'), CONCAT('good','_bad') from dual; select 'Hello' || 'World' from dual; -- ||연산자를 이용한 문자열 합치기 select sal || '원' from EMP; --단어의 첫 문자를 대문자로 변환 : INITCAP(char) select INITCAP('the soap') from dual; -- The Soap select INITCAP('good/bad morning') from dual; -- Good/Bad Morning --LOWER(char) / UPPER(char) select LOWER('ABC') from dual; select UPPER('abc') from dual; --LPAD() select LPAD('good',6) from dual; -- good이란 글자를 6자리로 늘림 select lpad('good',6,'@') "LPAD1" from dual; -- @@good 6자리로 확대하고 왼족에 3번째 -- 인자 문자를 채움 select lpad('good',8,'L') from dual; -- LLLLgood -- RPAD() select RPAD('good',6,'#') from dual; -- good## -- 단, 한글은 2바이트니까 주의해야한다. select RPAD('good','10','홍길동') from dual; -- good(4byte) + '홍길동'(6byte) 총 10byte필요. -- trim() : LTRIM(), RTRIM(), trim() select LTrim('goodbye','g') from dual; -- oodbye 왼쪽에 있는 g값을 지움 select lTrim('goodbye','o') from dual; -- goodbye 중간에 기여잇는 경우는 지울 수 없음 아래처럼 해야함 select lTrim('goodbye','go') from dual; -- dbye select RTrim('goodbye','go') from dual; -- goodbye 오른쪽에서 go를 찾으니까 없음으로 안지워짐 select RTrim('goodbye','e') from dual; -- 왼쪽 삭제 'leading' select trim(leading from ' good ') from dual; -- good select length(trim(leading from ' good ')) from dual; -- 5 : leading은 왼쪽기준 공백 제거함으로 뒤에 5개 길이가 나옴 -- 오른쪽 삭제 'trailing' select trim(trailing from ' good ') from dual; -- 양쪽 : 'both' select trim(both from ' good ') from dual; -- 양쪽 공백을 지움 -- 왼쪽기준에서 g문자를 지움 select trim(leading 'g' from 'good') from dual; -- ood --substr('문자열',시작인덱스,문자열개수(길이)) : 부분적인 문자열을 뽑아내는 함수(시작 인덱스가 1임!!) select substr('good morning john',8,4) from dual; -- rnin select substr('good morning john',6) from dual; -- morning john 길이를 생략하면 그 뒤로 쭉 다 출력된다. select substr('good morning john',-4) from dual; -- john 오른쪽 끝에서부터 거꾸로... select substr('good morning john',-4,2) from dual; -- jo select substr('good morning john',instr('good morning john','morning',1),length('morning')) from dual; --replace('원문자열','바꿀문자열','교체할문자열') : 문자열 교체 select replace('good morning john','good','switch') from dual; -- good문자열을 switch로 변경 : switch morning john --translate() : replace와 약간 다른데 -- translate('문자열','You','We')라하면 1:1로 대응해서 바끼게된다. Y는 W로 o는 e로 select translate('You are not alone','You','We') from dual; -- We are net alene -- ASCII() : 각 문자열의 아스키값을 구함 select ascii('a') from dual; -- 97 -- 문자열 길이 : length() select length('안녕') from dual; -- 2 -- instr('문자열','찾고자하는문자',어느인덱스부터찾을지,몇번째꺼찾을지) : 찾고자하는 문자열의 시작 위치를 반환(시작인덱스는 1부터...) select instr('good morning john','mor',1) from dual; -- 6 select instr('good morning john','n',1,2) from dual; -- 11 두번째 n시작위치를 구한다. -- 1번째 인덱스부터 찾기 시작해서 2번째 n의 위치를 가져오는 것 저작자표시 비영리