介绍字符型如下函数
ASCII('字符')
返回字符的ASCII码值idle> select ASCII('a') from dual; ASCII('A') ---------- 97 idle> select ASCII('A') from dual; ASCII('A') ---------- 65 idle>
CHR('n')
返回n的字符值 n是ASCII码数idle> select chr(65) from dual; C - A idle> select chr(39) from dual; C - ' idle> 但是求单引号的ASCII码写法很特殊 两个单引带表一个单引 idle> select ASCII(''') from dual; ERROR: ORA-01756: quoted string not properly terminated idle> select ASCII('''') from dual; ASCII('''') ----------- 39 idle>
LOWER(列名|表达式)
小写转换idle> select lower('ABC') from dual; LOW --- abc idle>
UPPER(列名|表达式)
大写转换idle> select upper('abc') from dual; UPP --- ABC idle>
INITCAP(列名|表达式)
每个词的词头大写 其他小写idle> select initcap('abC dEf XYZ') from dual; INITCAP('AB ----------- Abc Def Xyz idle>
CONCAT(列名|表达式,列名|表达式)
将第一个字符串和第二个字符串连接idle> select concat('abc','xyz') from dual; CONCAT ------ abcxyz 不太常用,因为我们可以用 || 连接 idle> select 'abc'||'xyz' from dual; 'ABC'| ------ abcxyz
SUBSTR(列名|表达式,m,[n])
返回指定子串,该子串是从第m个字符开始,其长度为n,不指定n值则从m到最后idle> select substr('abcdefxyz',4,3) from dual; SUB --- def idle> select substr('abcdefxyz',4) from dual; SUBSTR ------ defxyz idle>SELECT substr('12345678', -4) FROM dual; SUBSTR ------ 5678
LENGTH(列名|表达式)
返回字符串的长度idle> select length('abcdefxyz') from dual; LENGTH('ABCDEFXYZ') ------------------- 9 idle>
INSTR (列名|表达式,'字符串',[m],[n])
从表达式或列中搜索给定的字符串的所处位置,m代表从第几个开始搜,n代表第几次出现. m和n默认都是1,m=-1是从字符串后往前开始搜idle> select INSTR('abcdddxyz','d') from dual; INSTR('ABCDDDXYZ','D') ---------------------- 4 idle> idle> select INSTR('abcdddxyz','d',5) from dual; INSTR('ABCDDDXYZ','D',5) ------------------------ 5 idle> select INSTR('abcdddxyz','d',5,2) from dual; INSTR('ABCDDDXYZ','D',5,2) -------------------------- 6 idle>
TRIM([leading|]trailing|both 要去掉的字符 FROM 源字符串)
从源字符串中去掉指定的字符 可以用leading tailing来修饰去掉的字符串是在开头或结尾,默认是两者都 默认截取的是空格idle> select trim('a' from 'aaabcdeaaafxyzaaa') from dual; TRIM('A'FRO ----------- bcdeaaafxyz idle> select trim(leading 'a' from 'aaabcdeaaafxyzaaa') from dual; TRIM(LEADING'A -------------- bcdeaaafxyzaaa idle> select trim(trailing 'a' from 'aaabcdeaaafxyzaaa') from dual; TRIM(TRAILING' -------------- aaabcdeaaafxyz idle>
左补齐lpad 右补齐rpad
将不足20个字符的位置用指定符号填充.idle> select lpad(ename,20,'-') ,rpad(ename,20,'-') from emp where ename like 'S%'; LPAD(ENAME,20,'-') RPAD(ENAME,20,'-') -------------------- -------------------- ---------------SMITH SMITH--------------- ---------------SCOTT SCOTT--------------- idle>
REPLACE(源字符串,OLD字符串,NEW字符串)
从源字符串中找到搜索的old字符串,替换成new字符串idle> select replace('abcdefxyz','def','DEF') from dual; REPLACE(' --------- abcDEFxyz idle>
dump('str'[,FMT[,S,E]])
用于转换进制格式 str 被转换的字符串 FMT 格式 S str的开始字符位置 E S开始的后续结束字符位置SQL> select dump('abcd') from dual; DUMP('ABCD') -------------------------- Typ=96 Len=4: 97,98,99,100 SQL> SQL> select dump('abcd',1002) from dual; DUMP('ABCD',1002) ------------------------------------------------ Typ=96 Len=4 CharacterSet=AL32UTF8: 97,98,99,100 SQL> SQL> select dump('abcd',1008) from dual; DUMP('ABCD',1008) --------------------------------------------------- Typ=96 Len=4 CharacterSet=AL32UTF8: 141,142,143,144 SQL> DUMP('ABCD',1016) ----------------------------------------------- Typ=96 Len=4 CharacterSet=AL32UTF8: 61,62,63,64 SQL> select dump('abcd',1017) from dual; DUMP('ABCD',1017) ------------------------------------------- Typ=96 Len=4 CharacterSet=AL32UTF8: a,b,c,d SQL> SQL> select dump('abcd',1017,2,2) from dual; DUMP('ABCD',1017,2,2) --------------------------------------- Typ=96 Len=4 CharacterSet=AL32UTF8: b,c SQL> SQL> select dump('飞') from dual; DUMP('飞') ------------------------- Typ=96 Len=3: 233,163,158 SQL> select 233*power(256,2)+163*256+158 from dual; 233*POWER(256,2)+163*256+158 ---------------------------- 15311774 SQL> select chr(15311774) from dual; CHR --- 飞 SQL>
对比函数
- COALESCE 返回该表达式列表的第一个非空value。
select coalesce (1, null, 2 ) from dual ; -- 返回1 select coalesce ( null, 2, 1 ) from dual ; -- 返回2 select coalesce (t.empno, t.mgr ) from scott.emp t ; -- 效果类似 NVL( t.empno, t.mgr )
- GREATEST 返回值列表中最大值
select greatest (1, 3, 2 ) from dual ; -- 返回3 select greatest ( 'A', 'B', 'C' ) from dual ; -- 返回C select greatest (null, 'B', 'C' ) from dual ; -- 返回null select greatest (t.empno, t.mgr ) from scott.emp t ; -- 返回empno和mgr 较大值
- LEAST 返回值列表中最小值
select least (1, 3, 2 ) from dual ; -- 返回1 select least ( 'A', 'B', 'C' ) from dual ; -- 返回A select least (null, 'B', 'C' ) from dual ; -- 返回null select least (t.empno, t.mgr ) from scott.emp t ; -- 返回empno和mgr 较小值