1. 单行函数
字符、数字、日期、转换、混合型 等多种函数 用于处理单行数据 统称单行函数
均可用于SELECT WHERE ORDER BY 等子句中
也可以在其他语句中使用 如update的SET子句 INSERT的VALUES子句 DELET的WHERE子句
单行字符串函数用于操作字符串数据 大多数有一个或多个参数 绝大多数返回字符串
(1) NULL和单行函数
NULL值表示一个未知数据或者一个空值
算术操作符的任何一个操作数为NULL值 结果均为NULL值
只有CONCAT DECODE DUMP NVL REPLACE 在调用了NULL参数时能够返回非NULL值
NVL函数时最重要的 直接处理NULL值
NVL有两个参数:NVL(x1,x2) x1和x2都式表达式 当x1为null时返回X2,否则返回x1
( 不是简单加起来 如果某一行是null值 那么结果就将是null 要使用nvl函数来排除null值的影响
update empset salary=(salary+nvl(bonus,0)*1.1 )
(2) ASCII(' '):
String 是一字符串 返回String 第一个字母的ASCII码 : 逆函数是CHR()
SELECT ASCII(''A'') FROM table_name
(3) chr():
i 是一个数字 函数返回十进制表示的字符
SELECT CHR(65) FROM table_name
(4) concat( 'String1' , 'String2' ) :
String1, String2 均为字符串 将String1连接到String2后
String1为Null 返回String2 String2为Null 返回String1 均为Null 返回Null
(5) initcap( 'string1' , 'string2' .'string3')
String为字符串
将每个单词的第一个字母大写 其他字母小写返回
单词由空格 控制字符 标点符号限制
SELECT INITCAP( 'string1' , 'string2' ) FROM table_name
(6) instr( 'String1' ,'S', i , j ):
String1, String2为字符串 i, j为整数
返回String2在String1中第j次出现的位置 搜索从String1的第i个字符开始
当无需要字符返回0
若i为负 搜索从右向左 但位置计算仍从左向右 i,j缺省为1
SELECT INSTR('Miss', 'i', 1, 3) FROM table_name
instrb(String1, String2, i, j)
与instr() 相似 返回字节 对于单字节instrb()=instr()
(7) length(String1)
String1为字符串 返回String1的长度
如果String1为null 返回null值
select length('Tianjin University') from table ==> 18
lengthb()
与length()相似 返回字节
(8) lower(String)
返回String的小写字符 常出现在where子串中
select * from table
where lower(productfamily_no) like '%d6288_1%'
返回所有转换成小写形式为"d6288_1"(即在DB中是"D6288_1"或"d6288_1"形式的)
(9) upper(String)
返回String的大写 常出现在where子串中 与lower(String)
select productfamily_no from table
where upper(productfamily_no) like'%D6288_1%'
(10) lpad('String1', i,'String2')
i为整数 在String1左侧 用String2字符串 补足 致长度 i, 可重复多次
若i小于String1的长度 只返回 i 那么长的String1字符 其他的被截去
String2的缺省值为单空格
select lpad('Yes',2,'No') from table ==>"Ye"
select lpad('Yes',4,'No') from table ==>"NYes"
select lpad('Yes',5,'No') from table ==>"NoYes"
select lpad('Yes',7,'No') from table ==>"NoNoYes"
(11) rtrim('String1', 'String2')
将String1最右边的字符去掉 使其后第一个字符不在String2中
若无String2, 那么String1不会改变
select lpad('Yes','No') from table ==>"Yes"
select lpad('Yes','sNo') from table ==>"Ye"
select lpad('Yes','seNo') from table ==>"Y"
select lpad('Yes','yesNo') from table ==>"Y"
select lpad('Yes','YesNo') from table ==>" "
(12) replace('String1','String2','String3')
String1, String2, String3都是字符串
函数用String3代替出现在String1中的String2后返回
select repalce('YesNO', 'NO', 'No') from table ==>"YesNo"
(13) substr('String1', i, j)
String1为字符串, i, j为整数
从String1的第 i 位开始返回长度为 j 的字符串
如果 j 为空 则直到串的尾部
select substr('YesNo', 2, 3) from table ==>"esN"
substrb('String1', i, j)
与substr相似 只是 i, j以字节计算
(14) soundex('String1')
返回与String1发音相同的词
select soundex('Yes') yes, soundex('No') no from table
(15) translate('String1', 'String2', 'String3')
将String1与String2相同的字符以String3代替
select translate('Yos', 'o', 'e') from table ==>"Yes"
(16) trim( leading| trailing| both 'trim_character' from ' trim_source ')
若无其他参数 返回去掉前后空格的"trim_source"
否则返回处理好的"trim_source"
select trim(leading 'Y' from 'YesYesY') from table ==>"esYesY"
select trim(trailing 'Y' from 'YesYesY') from table ==>"YesYes"
select trim(both 'Y' from 'YesYesY') from table ==> "esYes"