Oracle—常见字符型处理函数

一、字符类型

类型 名称 最大长度 是否补齐 是否扩展 备注
char(n) 固定长度 2000 右端补齐  
varchar(n) 可变长度 4000  
varchar2(n) 可变长度 4000  

 

 

 ---

二、字符处理函数

1.lpad()、rpad()-补全字符串

语法:lpad(str,padded_len,[pad_str])

      rpad(str,padded_len,[pad_str])
--str 原字符;padded_len 改变后的字符长度,pad_str:以什么进行填充;

select lpad('1',4,'0') num from dual;
select lpad('12345',4,'0') num from dual;
select rpad('1',4,'$*') num from dual;

2.upper() lower()—大小写

select username,password from dba_users where upper(username)='nc1013';
select username,password from dba_users where lower(username)='nc1013';

3.initcap—单词首字母大写

select initcap('like') new_word from dual;
select initcap('LIKE') new_word from dual;
输出:Like
select initcap('we all like bike') new_word from dual; 
输出:We All Like Bike

select initcap('we-all-like-bike') new_word from dual; 输出:We-All-Like-Bike

4.substr()—字符串截取

语法:substr(str,start_idx,length)
     str 原始字符串 start_idx开始截取位置,length 截取长度
select substr('123456789',2,3) new_word from dual;    
输出:234
select substr('123456789',2) new_word from dual;
输出:23456789

5.instr()—获取字符串位置

语法:instr(str,sub_str[,index][,times])
     str;字符串;sub_str index:搜索开始位置 times 出现第几次
select instr('123456789','56') pos from dual;
输出:5

select instr('123456789','56',7) pos from dual;
输出:0
select instr('123456789','56',1,2) pos from dual;
输出:0

6.ltrim()、rtrim()、trim()空格处理

select ltrim('   000') new_str from dual;
输出:000
select rtrim(' 000 ') new_str from dual;
输出: 000
select trim(' 000 ') new_str from dual;
输出:000

7.concat()—字符串拼接

select concat('Hello',' World') from dual;   
输出:Hello World
select concat('Hello ',concat('Hello',' World')) new_str from dual;
输出:Hello Hello World

select 'Hello '||'  Hello'||' World' new_str from dual; 
输出:Hello   Hello World

8.translate()—翻译

select translate('+-*','1+23-4*','abcdefghijklmnopqrstuvwxyz') trans from dual;
输出:beg

---提取字符串中数字
select translate('123dsfj5643jddh','abcdefghijklmnopqrstuvwxyz',' ') trans from dual
输出:1235643

---提取字符串中字母
select translate('123dsfj5643jddh','0123456789',' ') trans from dual
输出:dsfjjddh
原文地址:https://www.cnblogs.com/zs-chenkang/p/13899152.html