返回字符的字符函数
1、CHR(n) [n为正整数,如果n>256,就去MOD(n,256)]
select CHR(65) a1,CHR(67)||CHR(65)||CHR(84) a2 FROM DUAL;
2、CONCAT(ch1,ch2) 拼接字符串[cha1,ch2为任意字符CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, NCLOB]
select CONCAT('A','1') a1,CONCAT('张三','A') a2 from dual;
CONCAT(CLOB, NCLOB) returns NCLOB
CONCAT(NCLOB, NCHAR) returns NCLOB
CONCAT(NCLOB, CHAR) returns NCLOB
CONCAT(NCHAR, CLOB) returns NCLOB
3、INITCAP(ch) 单次首字母大写,其他小写
select INITCAP('hello world DBA') a1 from dual;
4、LOWER(ch) 将字符串转换为小写
select LOWER('HEllo World DBA') a1 from dual;
5、LPAD(expr1,n,expr2) 从expr1中截取n个字符返回,如果长度不够就用expr2填充左边
select LPAD('Hello World',21,'ABC') a1 from dual;
6、LTRIM(ch,set)将字符串ch左边的包含在set中的字符移除,如股票不指定set,就为空格,中间有其他间隔就停止截取
select LTRIM('B_AHello','AB_') a1,LTRIM('B_FAHello','AB_') a2 from dual;
7、NCHR(n) 相当于 CHR(n USINGNCHAR_CS)
select NCHR(67) a1,NCHR(1458) a2from dual;
8、NLS_INITCAP(ch,nlspara)针对字符串单词首字母大写,其余小写,可以指定排序规则nlspara,也可以不指定采用默认规则
select NLS_INITCAP('hello world') a1, NLS_INITCAP('hello world','NLS_SORT=XDutch') a2from dual;
9、NLS_LOWER(ch, nlspara)将字符串转换为小写字母,可以指定排序规则
SELECT NLS_LOWER('Hello World') a1,NLS_LOWER('NOKTASINDA','NLS_SORT =XTurkish') a2FROM DUAL;
10、NLS_UPPER(ch, nlspara)将字符串转换为大写字母,可以指定排序规则
SELECT NLS_UPPER ('Hello World') a1,NLS_UPPER ('NOKTASINDA','NLS_SORT = XTurkish') a2FROM DUAL;
11、NLSSORT(ch, nlspara)返回字符串的字节码。用于排序
拼音:SELECT * FROM表名 ORDER BY NLSSORT(字段名,'NLS_SORT = SCHINESE_PINYIN_M')
笔划:SELECT * FROM表名 ORDER BY NLSSORT(字段名,'NLS_SORT = SCHINESE_STROKE_M')
部首:SELECT * FROM表名 ORDER BY NLSSORT(字段名,'NLS_SORT = SCHINESE_RADICAL_M')
12、REGEXP_REPLACE(ch_source,pattern)正则表达式替换
SELECT REGEXP_REPLACE(phone_number,'([[:digit:]]{3}).([[:digit:]]{3}).([[:digit:]]{4})','(1) 2-3') a1 FROM employees;
SELECT REGEXP_REPLACE('500 Oracle Parkway,Redwood Shores, CA','( ){2,}', ' ') a1FROM DUAL;
13、REGEXP_SUBSTR(ch_source,pattern)正则表达式截取
SELECT REGEXP_SUBSTR('500 Oracle Parkway,Redwood Shores, CA',',[^,]+,') a1 FROM DUAL;
14、REPLACE(ch,search_string,replace_string)字符串替换
SELECT REPLACE('JACK and JUE','J','哈哈') a1 FROM DUAL;
15、RPAD(expr1,n,expr2) 从expr1中截取n个字符返回,如果长度不够就用expr2填充右边,默认填补空格
select RPAD('Hello World',21,'ABC') a1, RPAD('Hello World',21) a2 from dual;
16、RTRIM(ch,set) 将字符串ch右边的包含在set中的字符移除,如股票不指定set,就为空格,中间有其他间隔就停止截取
select RTRIM('HelloSSL_B','AB_') a1,RTRIM('B_FAHello_','AB_') a2 from dual;
17、SOUNDEX(ch) 返回字符串参数的语音表示形式,相对于比较一些读音相同,但是拼写不同的单词是非常有用的,在中文环境中没多大用处
18、SUBSTR(ch,position,length)截取字符串
select substr('Hello World',1,2) a1,substr('Hello World',2) a2 from dual;
19、TRANSLATE(ch,from_string,to_string)与REPLACE函数功能类似
针对ch字符串,将from_string中的字符串一一替换为to_string
select translate('123abc','2dc','4e') a1,translate('123abc','2','4e')a2from dual;
translate('123abc','2dc','4e'):将会把2->4,d->e,c->’’
translate('123abc','2','4e'):将会把2->4
20、TREAT
21、TRIM(ch fromsource_string) 去掉两边ch
TRIM(LEADINGch fromsource_string) 去掉左边ch
TRIM(TRAILINGch fromsource_string) 去掉右边ch
TRIM(BOTHch fromsource_string) 去掉两边 ch
select TRIM(LEADING'H'from'Hello World') a1,TRIM(TRAILING'd'from'Hello World') a2,TRIM(BOTH'H'from'Hello H WorldH') a3 from dual;
select TRIM('A'from'A Hello World A') a1 from dual;
22、UPPER(ch) 字符转换为大写
select upper('Hello World') a1 from dual;
字符函数返回数值
1、ASCII(ch) 返回字符的ASCII码,ch可以是CHAR,VARCHAR2, NCHAR, or NVARCHAR2
select ASCII('A') a1,ASCII('张') a2 from dual;
2、INSTR(source,ch,position,n)在source中从位置position开始搜索ch,第n次出现的位置,没有匹配的返回0,默认从1开始第一次出现
select INSTR('Hello World','o',3,2) a1,INSTR('Hello World','o') a2 from dual;
3、LENGTH(ch) 返回字符串长度
select LENGTH('Hello张三') a1from dual;
4、REGEXP_COUNT(source_string,pattern,position,match_para)
正则表达式规则匹配字符串在原字符串中出现的次数
SELECT REGEXP_COUNT('123123123123123','(12)3',1,'i') a1FROM DUAL;
5、REGEXP_INSTR(source_string,pattern)正则表达式匹配字符串位置
SELECT REGEXP_INSTR('500 Oracle Parkway,Redwood Shores, CA','[^ ]+', 1,6) a1FROM DUAL;
NLS字符函数
1、NLS_CHARSET_DECL_LEN(byte_count,char_set_id)
返回一个 NCHAR 列的声明长度(也就是字符个数)。byte_count 参数是列的宽度。'char_set_id' 参数是字符集 ID
SELECT NLS_CHARSET_DECL_LEN(200, nls_charset_id('ja16eucfixed')) a1 FROM DUAL;
2、NLS_CHARSET_ID(string)返回字符集名称 对应的字符集ID
SELECT NLS_CHARSET_ID('ja16euc') a1FROM DUAL;
3、NLS_CHARSET_NAME(number)返回字符集 ID对应的字符集名称
SELECT NLS_CHARSET_NAME(831) a1FROM DUAL;