一、单行函数
1.定义:对表或视图的查询时,针对每行记录返回一个值的函数。
2.用途:用于select语句,where条件
3.分类:
数值函数 Number Functions
字符函数(返回字符) Character Functions Returning Character Values
字符函数(返回数值) Character Functions Returning Number Values
日期函数 Datetime Functions
转换函数 Conversion Functions
其它单值函数 Miscellaneous Single-Row Functions
二、数值函数
数值函数:接收一个数值参数,返回一个数值结果的函数。
常用函数:
1.MOD(m,n) m/n取余函数
SELECT MOD(11,4) "Modulus" FROM DUAL;
2.ABS(n) 取绝对值函数
SELECT ABS(-15) "Absolute" FROM DUAL;
3.SIGN(n) 符号函数,正数返回1,负数返回-1,0返回0
SELECT SIGN(-15) "Sign" FROM DUAL;
4.CEIL(n) 向上取整函数
SELECT CEIL(15.7) "Ceiling" FROM DUAL;
5.FLOOR(n) 向下取整函数
SELECT FLOOR(15.7) "Floor" FROM DUAL;
6.ROUND(m)/ROUND(m,n) 四舍五入函数
SELECT ROUND(15.193) "Round" FROM DUAL;
SELECT ROUND(15.193,1) "Round" FROM DUAL;
SELECT ROUND(15.193,-1) "Round" FROM DUAL;
7.TRUNC(m)/TRUNC(m,n) 截断函数
SELECT TRUNC(15.79) "Truncate" FROM DUAL;
SELECT TRUNC(15.79,1) "Truncate" FROM DUAL;
SELECT TRUNC(15.79,-1) "Truncate" FROM DUAL;
不常用函数:
1.POWER(m,n) m的n次方函数
SELECT POWER(10,2) "Raised" FROM DUAL;
2.SQRT 平方根函数
SELECT SQRT(25) "Square root" FROM DUAL;
3.LOG(m,n) n的m次方根函数
SELECT LOG(10,100) "Log base 10 of 100" FROM DUAL;
SELECT LOG(10,POWER(10,2)) "Log base 10 of 100" FROM DUAL;
4.EXP e(e = 2.71828183)的m次方函数
SELECT EXP(4) "e to the 4th power" FROM DUAL;
5.LN m的e(e = 2.71828183)次方根函数
SELECT LN(exp(4)) "Natural log of exp(4)" FROM DUAL;
6.SIN 正玄函数
SELECT SIN(30 * 3.14159265359/180) "Sine of 30 degrees" FROM DUAL;
7.COS 余玄函数
SELECT COS(60 * 3.14159265359/180) "Cosine of 60 degrees" FROM DUAL;
8.TAN 正切函数
SELECT TAN(45 * 3.14159265359/180) "Tangent of 45 degrees" FROM DUAL;
9.ASIN 反正玄函数
SELECT ASIN(0.5) "Arc_Sine" FROM DUAL;
SELECT ASIN(SIN(30 * 3.14159265359/180)) "Arc_Sine" FROM DUAL;
10.ACOS 反余玄函数
SELECT ACOS(COS(60 * 3.14159265359/180)) "Arc_Cosine" FROM DUAL;
11.ATAN 反正切函数
SELECT ATAN(TAN(45 * 3.14159265359/180)) "Arc_Tangent" FROM DUAL;
12.ATAN2
SELECT ATAN2(0.3, 0.2) "Arc_Tangent2" FROM DUAL;
13.SINH 双曲正玄
SELECT SINH(1) "Hyperbolic sine of 1" FROM DUAL;
14.COSH 双曲余玄函数
SELECT COSH(0) "Hyperbolic cosine of 0" FROM DUAL;
15.TANH 双曲正切
SELECT TANH(.5) "Hyperbolic tangent of .5" FROM DUAL;
三、字符函数(返回字符) Character Functions Returning Character Values
1.CHR(n) 将数字转换为ASCII码
select CHR(65) from dual;
2.CONCAT(m,n) 连接字符串,等同于连接操作符||
select CONCAT(‘A‘,‘B‘) from dual;
select CONCAT(CONCAT(‘A‘,‘B‘),‘C‘) from dual;
select ‘A‘||‘B‘||‘C‘ from dual;
3.INITCAP(n) 将字符中单词的首字母大写
SELECT INITCAP(‘hello sql‘) "Capitals" FROM DUAL;
4.UPPER(n) 将字符中字母大写
SELECT UPPER(‘hello sql‘) "Uppercase" FROM DUAL;
5.LOWER(n) 将字符中字母小写
SELECT LOWER(‘HELLO SQL‘) "Lowercase" FROM DUAL;
6.LPAD(c1,n,c2) 从左侧补齐字符串的长度
SELECT LPAD(‘Page 1‘,15,‘*.‘) "LPAD example" FROM DUAL;
SELECT LPAD(‘Page 1‘,15) "LPAD example" FROM DUAL;
7.RPAD(c1,n,c2) 从右侧补齐字符串的长度
SELECT RPAD(‘Page 1‘,15,‘*.‘) "LPAD example" FROM DUAL;
SELECT RPAD(‘Page 1‘,15) "LPAD example" FROM DUAL;
8.TRIM 修剪前后两端的空格或指定字符
SELECT TRIM (‘ AB C ‘) "TRIM Example" from dual;
--SELECT TRIM (‘00098702348900‘,‘0‘) "TRIM Example" from dual;
SELECT TRIM (0 FROM 00098702348900) "TRIM Example" from dual;
9.LTRIM 修剪前端的空格或指定字符
SELECT LTRIM (‘ AB C ‘) "LTRIM Example" from dual;
SELECT LTRIM (‘00098702348900‘,‘0‘) "LTRIM Example" from dual;
10.RTRIM 修剪后端的空格或指定字符
SELECT RTRIM (‘ AB C ‘) "RTRIM Example" from dual;
SELECT RTRIM (‘00098702348900‘,‘0‘) "RTRIM Example" from dual;
11.SUBSTR(c,p,l) 从字符串指定位置截取指定长度的子字符串
SELECT SUBSTR(‘ABCDEFG‘,3,4) "Substring" FROM DUAL;
SELECT SUBSTR(‘ABCDEFG‘,3) "Substring" FROM DUAL;
SELECT SUBSTR(‘ABCDEFG‘,-3) "Substring" FROM DUAL;
SELECT SUBSTR(‘ABCDEFG‘,-3,4) "Substring" FROM DUAL;
SELECT SUBSTR(‘ABCDEFG‘,1,2) "Substring" FROM DUAL;
SELECT SUBSTR(‘ABCDEFG‘,0,2) "Substring" FROM DUAL;
SELECT SUBSTR(‘我是老师‘,1,4) "Substring" FROM DUAL;
SELECT SUBSTRB(‘我是老师‘,1,4) "Substring" FROM DUAL;
SELECT SUBSTRB(‘我是老师‘,1,3) "Substring" FROM DUAL;
SELECT SUBSTRB(‘A我B是C老F师G‘,1,3) "Substring" FROM DUAL;
SELECT SUBSTRC(‘我是老师‘,1,3) "Substring" FROM DUAL;
SELECT SUBSTR(‘A我B是C老F师G‘,1,3) "Substring" FROM DUAL;
SELECT SUBSTRC(‘A我B是C老F师G‘,1,3) "Substring" FROM DUAL;
12.REPLACE(c,sc,rc) 从c字符串中找到sc子串并用rc子串进行替换
SELECT REPLACE(‘JACK and JUE‘,‘J‘,‘BL‘) "Changes" FROM DUAL;
SELECT REPLACE(‘我是老师‘,‘‘,‘*‘) FROM DUAL;
SELECT REPLACE(‘我是老师‘,‘‘) FROM DUAL;
13.TRANSLATE(c,from_c,to_c) 格式转化,按照from_c到to_c的模式转化字符串c
SELECT TRANSLATE(‘2KRW229‘,‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ‘,
‘9999999999XXXXXXXXXXXXXXXXXXXXXXXXXX‘) "License" FROM DUAL;
SELECT TRANSLATE(‘2KRW229‘,‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ‘,
‘0123456789‘) FROM DUAL;
select TRANSLATE(‘2018‘,‘0123456789‘,‘零一二三四五六七八九‘) from dual;
select TRANSLATE(‘1588‘,‘0123456789‘,‘零壹贰叁肆伍陆柒捌玖‘) from dual;
四、字符函数(返回数值) Character Functions Returning Number Values
1.ASCII(c) 返回字符的ASCII码
SELECT ASCII(‘A‘) FROM DUAL;
2.INSTR(string,substring,position,occurrence)
查找,返回子字符串的位置
SELECT INSTR(‘CORPORATE FLOOR‘,‘O‘) "Instring" FROM DUAL;
SELECT INSTR(‘CORPORATE FLOOR‘,‘OR‘) "Instring" FROM DUAL;
SELECT INSTR(‘CORPORATE FLOOR‘,‘OR‘,3) "Instring" FROM DUAL;
SELECT INSTR(‘CORPORATE FLOOR‘,‘OR‘,-3) "Instring" FROM DUAL; --位置参数是负数时,从右向左找。
SELECT INSTR(‘CORPORATE FLOOR‘,‘OR‘,3,2) "Instring" FROM DUAL;
SELECT INSTR(‘CORPORATE FLOOR‘,‘OR‘,-3,2) "Instring" FROM DUAL;
select ename,INSTR(ename,‘O‘) ,substr(ename,INSTR(ename,‘O‘)) from emp
3.LENGTH(c) 返回字符串长度
SELECT LENGTH(‘ABCDEFG‘) "Length in characters" FROM DUAL;
SELECT LENGTH(‘我是老师‘) "Length in characters" FROM DUAL;
SELECT LENGTHB(‘ABCDEFG‘) "Length in bytes" FROM DUAL;
SELECT LENGTHB(‘我是老师‘) "Length in bytes" FROM DUAL;
五、日期函数 Datetime Functions
日期 +/- 数字 = 日期
SELECT sysdate+5,sysdate-10 FROM DUAL;
日期 - 日期 = 数字
SELECT sysdate-TO_DATE(‘2017-08-16‘,‘yyyy-mm-dd‘) FROM DUAL;
1.SYSDATE 当前日期 SYSTIMESTAMP、CURRENT_DATE、
CURRENT_TIMESTAMP、LOCALTIMESTAMP
select sysdate from dual;
select sysdate,SYSTIMESTAMP,CURRENT_DATE,
CURRENT_TIMESTAMP,LOCALTIMESTAMP from dual;
2.LAST_DAY(date) 取本月最后1天
select sysdate,last_day(sysdate) from dual;
3.NEXT_DAY(date,n) 获取下个星期几的日期
select sysdate,next_day(sysdate,1) from dual;
4.ADD_MONTHS(d,n) 月份加减数字
select sysdate,add_months(sysdate,1),add_months(sysdate,-2),
add_months(sysdate,0.5) from dual;
5.MONTHS_BETWEEN(d1,d2) 月份相减
SELECT MONTHS_BETWEEN(sysdate,TO_DATE(‘2017-08-16‘,‘yyyy-mm-dd‘)) "Months" FROM DUAL;
6.EXTRACT (类型 from date) 从日期中提取年月日时分秒
SELECT EXTRACT(YEAR FROM DATE ‘2017-03-07‘) FROM DUAL;
SELECT EXTRACT(MONTH FROM sysdate) FROM DUAL;
SELECT EXTRACT(DAY FROM sysdate) FROM DUAL;
SELECT EXTRACT(HOUR FROM LOCALTIMESTAMP) FROM DUAL;
SELECT EXTRACT(MINUTE FROM LOCALTIMESTAMP) FROM DUAL;
SELECT EXTRACT(SECOND FROM LOCALTIMESTAMP) FROM DUAL;
7.TRUNC(date) 日期截断
SELECT TRUNC(sysdate, ‘yyyy‘) "New Year" FROM DUAL;
SELECT TRUNC(sysdate, ‘mm‘) "New Months" FROM DUAL;
SELECT TRUNC(sysdate, ‘dd‘) "New Day" FROM DUAL;
SELECT TRUNC(sysdate, ‘hh24‘) "New Hour" FROM DUAL;
SELECT TRUNC(sysdate, ‘mi‘) "New Minute" FROM DUAL;
8.ROUND(date) 日期四舍五入
SELECT ROUND(sysdate, ‘yyyy‘) "New Year" FROM DUAL;
SELECT ROUND(sysdate, ‘mm‘) "New Year" FROM DUAL;
SELECT ROUND(sysdate, ‘dd‘) "New Year" FROM DUAL;
SELECT ROUND(sysdate, ‘hh24‘) "New Year" FROM DUAL;
SELECT ROUND(sysdate, ‘mi‘) "New Year" FROM DUAL;
六、转换函数 Conversion Functions
转换函数:将数值从一个类型转换到另一个类型。
1.BIN_TO_NUM 二进制转数字
SELECT BIN_TO_NUM(1,0,1,0) FROM DUAL;
2.TO_CHAR (datetime) 日期转字符串
SELECT to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘) from dual;
3.TO_DATE 字符串转日期
SELECT sysdate - to_date(‘2017-01-01‘,‘yyyy-mm-dd‘) from dual;
4.TO_NUMBER 字符转数字
SELECT to_number(‘012345‘) from dual;
SELECT to_number(‘0123.45‘,‘9999D99‘) from dual;
SELECT to_number(‘$0123.45‘,‘$9999D99‘) from dual;
七、其它单值函数 Miscellaneous Single-Row Functions
1.DECODE 条件选择函数
SELECT emp.*,decode(deptno,10,‘ACCOUNTING‘,
20,‘RESEARCH‘,30,‘SALES‘,‘OPERATIONS‘) from emp;
SELECT emp.*,decode(sign(sal-1600),
1,‘工资大于1600‘,0,‘工资等于1600‘,-1,‘工资小于1600‘) from emp;
SELECT emp.*,decode(sign(instr(ename,‘A‘)),
1,‘名字包含A‘,0,‘名字不包含A‘) from emp;
2.coalesce (expr1, expr2, ..., exprn) 返回首个非空表达式
SELECT emp.*,COALESCE (comm,0) from emp;
3.NVL(c,v) NVL2(c,v1,v2) 空值转化
SELECT emp.*,sal+comm,sal+nvl(comm,0) from emp;
SELECT emp.*,nvl2(comm,sal+comm,sal) from emp;
4.USER 返回当前用户名\ID
SELECT USER from dual;
SELECT UID FROM DUAL;
5.SYS_GUID 返回由16字节组成的全局唯一标识符(原始值)。
SELECT SYS_GUID() from dual;
6.NULLIF(s1,s2) s1与s2相等时返回空,不等时返回s1
SELECT emp.deptno,dept.deptno,
nullif(emp.deptno,dept.deptno) from emp,dept;
7.GREATEST 返回集合中最大值
SELECT GREATEST (‘A‘, ‘B‘, ‘C‘) "Greatest" FROM DUAL;
8.LEAST 返回集合中最小值
SELECT LEAST (‘A‘, ‘B‘, ‘C‘) "least" FROM DUAL;
原文地址:https://www.cnblogs.com/BradMiller/p/9279548.html