单行函数的语法数值型函数字符函数日期函数转换函数聚合函数
单行函数的语法
function_name(column|expression, [arg1, arg2,
...])
说明:
参数说明
function_name 函数名称 column 列名 expression 表达式 arg1,arg2,… 参数
数值型函数
abs(x) 返回 x 的绝对值
select abs(-2), abs(2) from dual;
sign(x) 返回 x 的正负值,若为正值返回 1,负值返回 - 1,0 返回 0
select sign(-
12),
sign(
23),
sign(
0) from dual;
power(x,y) 返回 x 的 y 次幂
select power(2, 3) from dual;
sqrt(x) 返回 x 的平方根
select sqrt(2) from dual;
mod(x,y) 返回 x 除以 y 的余数
select mod(5, 3) from dual;
round(x[,y]) 返回 x 按精度 y 截取后的值 若不指定 y,则保留 x 整数部分 如果 y 不为整数,则截取 y 整数部分 如果 y>0,则四舍五入为 y 位小数 如果 y 小于 0,则四舍五入截取到小数点向左第 y 位
select round(567.567, 2), round(567.567, -2) from dual;
trunc(x[,y]) 返回截取后的值,用法同 round(x[,y]),只是不做四舍五入
select trunc(567.567, 2), trunc(567.567, -2) from dual;
字符函数
concat(c1,c2) 连接两个字符串
select concat('hello ', 'world') from dual;
initcap(c1) 将字符串的第一个字母变为大写,其它字母小写
select initcap('hello world') from dual;
lower(c1) 将字符串全部转为小写
select lower('Hello World') from dual;
uppper(c1) 将字符串全部转为大写
select upper('Hello World') from dual;
length(c1) 返回字符串的长度(个数)
select length('hello 世界') from dual;
lengthB(c1) 返回字符串的长度(byte)
select lengthB('hello 世界') from dual;
replace(c1,c2[,c3]) 将字符表达式值中,部分相同字符串,替换成新的字符串 用字符串 c3 替换字符串 c1 中的字符串 c2 若不指定 c3 默认为空,即删除
select replace('hello world', 'world', 'oracle') from dual;
substr(c1,n1[,n2]) 取子字符串,在字符表达式 c1 里,从 n1 开始取 n2 个(个数)字符 若不指定 n2,则从第 y 个字符直到结束的字串
select substr('HelloWorld', 6, 2) from dual;
substrB(c1,n1[,n2]) 用法同 substr,长度表示不同(byte)
select substr('HelloWorld', 6, 2) from dual;
日期函数
sysdate 返回当前日期
select sysdate from dual;
add_months(d1,n1) 返回在日期 d1 基础上再加 n1 个月后新的日期
select add_months(sysdate, 12) from dual;
months_between(d1,d2) 返回日期 d1 到日期 d2 之间的月数 如果 d1 大于 d2,则返回正数,反之返回负数
select months_between(sysdate, todate('2016-05-23', 'yyyy-mm-dd')) from dual;
注 日期 - 数字 = 日期 日期 + 数字 = 日期 日期 - 日期 = 数字 表示两个日期之间相隔的天数
转换函数
TO_CHAR(x[[,c2],C3]) 将日期或数据转换为 char 数据类型,详见 Oracle to_char 格式化函数
select to_char(sysdate, 'yyyy-mm') from dual;
to_date(x[, c2[, c3]]) 将字符串 x 转化为日期型,参数参照 to_char()
select to_date('2017-03-14', 'yyyy-mm-dd') from dual;
to_number(x[[, c2], c3]) 将字符串 x 转化为数字型,参数参照 to_char()
select to_number('123456') + 1 from dual;
注 若使用to_date()时给定日期参数不够,则默认补全为当月1日中午12:00:00
select to_date('2016', 'yyyy') from dual;
聚合函数
avg([distinct | all] x) 统计数据表选中行 x 列的平均值
select avg(salary), avg(distinct salary) from employees;
sum([distinct | all] x) 统计数据表选中行 x 列的合计值
select sum(salary), sum(distinct salary) from employees;
count(* | [distinct | all] x) 统计数据表选中行 x 列的合计值
select count(*), count(distinct salary) from employees;
max([distinct | all] x) 统计数据表选中行 x 列的最大值 all 表示对所有的值求最大值, distinct 只对不同的值求最大值,默认为 all
select max(salary), max(distinct salary) from employees;
min([distinct | all] x) 统计数据表选中行 x 列的最小值 all 表示对所有的值求最小值, distinct 只对不同的值求最小值,默认为 all
select min(salary), min(distinct salary) from employees;
转载请注明原文地址: https://ju.6miu.com/read-33007.html