Oracle基础教程:单行函数—字符类型函数
字符型
ASCII
CHR
LOWER
UPPER
INITCAP
CONCAT
SUBSTR
LENGTH
INSTR
TRIM
dump
lpad
rpad
REPLACE
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>
dual 为虚表,当一个语句不需要从表中获取数据时,但又要维持SQL语法,Oracle才提供了虚表来解决这个问题
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
idle>
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>
LENGTH(列名|表达式)
返回字符串的长度
idle> select length('abcdefxyz') from dual;
LENGTH('ABCDEFXYZ')
-------------------
9
idle>
INSTR (列名|表达式,'字符串',[m],[n])
从表达式或列中搜索给定的字符串的所处位置,m代表从第几个开始搜,n代表第几次出现. m和n默认都是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>