一、单行函数
单行函数对于表中查询的每一行只返回一个值,可出现在select子语句和where子语句中。
1、字符函数
函数 解释 输入例子 输出结果 initcap(字符串) 首字母大写 initcap(‘hello’) Hello lower(字符串) 所有字母转换成小写字母 lower(‘ABC’) abc upper(字符串) 所有字母转换成大写字母 lower(‘abc’) ABC length(字符串) 求字符串长度 length(‘abc’) 3 substr(字符串,开始位,长度) 从第几位开始截取子字符串 substr(‘abcde’,2,3) bcd replace(字符串,被替换字符,替换字符) 替换字符 replace(‘abasd’,‘a’,‘x’) xbxsd concat(字符串1,字符串2) 合并字符 concat(‘hello’,‘world’) helloworld chr(数字) 将数字转换成ASCII码表中的字符 chr(97) a ascii(字符) 将字符转换成ASCII码表中的数字 ascii(‘a’) 97 lpad(字符,位数,‘填充字符’) 使用固定字符填充到固定长度 lpad(‘abc’,5,‘x’) xxabc
2、日期函数
函数 解释 输入例子 输出结果 add_months(日期,月数) 多少个月后的日期 add_months(‘05-6月 -21’,5) 05-11月 -21 months_between(日期1,日期2) 两个日期相差多少个月,不满整月会出现小数 months_between(‘05-6月 -21’,‘05-11月 -21’) 5 extract(year from 日期) 从一个日期中提取年 extract(year from ‘05-6月 -21’) 21 extract(month from 日期) 从一个日期中提取月 extract(month from ‘05-6月 -21’) 6 extract(day from 日期) 从一个日期中提取天 extract(day from ‘05-6月 -21’) 5 last_day(日期) 所在月的最后一天 last_day(‘05-6月 -21’) 30-6月 -21
select * from students where add_month( birthday, 12 * 20 ) < sysdate;
select * from students where last_day( birthday) - 10 = birthday;
3、数字函数
函数 解释 输入例子 输出结果 abs(数字) 求绝对值 abs(-1) 1 ceil(数字) 向上取整 ceil(10.01) 11 sin(数字) 正弦函数 sin(0) 0 cos(数字) 余弦函数 cos(0) 1 power(数字,幂) 指数函数 power(2,3) 8 round(数字,保留小数点后几位) 四舍五入 round(4.12,1) 4.1 sqrt(数字) 求平方根 sqrt(9) 3 floor(数字) 向下取整 floor(5.9) 5
4、转换函数
函数 解释 输入例子 输出结果 to_char(数字,‘转换格式’) 数字转换成字符 to_char(12,‘L99.99’) ¥12.00 to_date(字符,‘转换格式’) 数字转换成日期 to_date(‘20210727’,‘yyyymmdd’) 27-7月-21 to_number(字符) 字符转换成数字 to_number(‘12’) 12
select to_char( 12 , 'l99.99' ) from dual;
select to_char( 12 , '$99.99' ) from dual;
SQL > select to_char( 1.1 , '99.999' ) from dual;
TO_CHAR( 1.1 , '99.999' )
1.100
SQL > select to_char( 1.1 , '09.999' ) from dual;
TO_CHAR( 1.1 , '09.999' )
01.100
5、混合函数
函数 解释 输入例子 输出结果 nvl(表达式1,表达式2) 第一个为空返回第二个,否则返回第一个 nvl(1,2) 1 nvl2(表达式1,表达式2,表达式3) 第一个不空返回第二个,否则返回第三个 nvl2(1,2,3) 2 nullif 两个表达式相等则返回空,否则返回第一个 nullif(2,2) null
6、分组函数
函数 解释 输入例子 输出结果 avg() 求平均数 min() 求最小值 max() 求最大值 sum() 求和 count() 计数