当前位置: 移动技术网 > IT编程>数据库>Oracle > [学习笔记] Oracle字符串函数、日期函数、数值函数、转换函数、聚合函数

[学习笔记] Oracle字符串函数、日期函数、数值函数、转换函数、聚合函数

2020年03月16日  | 移动技术网IT编程  | 我要评论

时尚美甲图案,卡宾枪,北京工体夜店歌曲

函数

  • 单行函数:对一行数据进行操作的函数,如字符串函数、数值函数、转换函数、日期函数等。
  • 聚合函数:同时对多行数据进行操作,如求和函数等。

字符串函数

函数 说明
ascii(x) 求字符x的ascii码
chr(x) 求ascii码对应的字符
length(x) 求字符串x的字符长度
concata(x,y) 连接x和y两个字符串
instr(x,y[,start]) 查找在字符串x中字符串y的位置,可以指定从start位置开始搜索,默认从头开始
lower(x) 把字符串中大写字母转换为小写
upper(x) 把字符串中小写字母转换为大写
initcap(x) 把字符串中所有单词首字母转换为大写,其余小写
ltrim(x[,y]) 去掉字符串x左边的y字符串,y不填时,默认的是字符串x左边去空格
rtrim(x[,y]) 去掉字符串x右边的y字符串,y不填时,默认的是字符串x右边去空格
trim(x[,y]) 去掉字符串x两边的y字符串,y不填时,默认的是字符串x左右去空格
replace(x,old,new) 将字符串中的old字符替换为new字符
substr(x,start[,length]) 截取字符串x,从start位置开始截取长度为length的字符串,length不填默认为截取到字符串x末尾
rpad(x,length[,y]) 对字符串x进行右补字符y使字符串长度达到length长度
lpad(x,length[,y]) 对字符串x进行左补字符y使字符串长度达到length长度

日期函数

函数 说明
sysdate() 系统的当前时间
systimestamp() 系统的当前时间,包含时区信息,精确到微秒
dbtimezone() 返回数据库时区
add_months(r,n) 返回在指定日期r上加上月份n后的日期,n可以为负数
last_day(r) 返回指定r日期的当前月份的最后一天的日期
next_day(r,c) 返回指定r日期后的星期几c对应的日期
extract(time) 返回指定time时间当中的年月日、时分秒等特定部分
months_between(r1,r2) 返回两个日期间的月份数,结果是正数、负数、小数
round(r[,f]) 将日期r按f的格式进行四舍五入
trunc(r[,f]) 将日期r按f的格式进行截取
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
select systimestamp from dual;
select dbtimezone from dual;

-- 结果为 2018-11-30,2018-10-31
select 
    to_char(add_months(to_date('2018-10-31','yyyy-mm-dd'),1),'yyyy-mm-dd'), 
    to_char(add_months(to_date('2018-09-30','yyyy-mm-dd'),1),'yyyy-mm-dd')
from dual;

select last_day(sysdate) from dual;

select next_day(to_date('2018-11-12','yyyy-mm-dd'),'星期四') from dual;

select extract(year from timestamp '2018-11-12 15:36:01') as year,
       extract(month from timestamp '2018-11-12 15:36:01') as month,
       extract(day from timestamp '2018-11-12 15:36:01') as day,  
       extract(minute from timestamp '2018-11-12 15:36:01') as minute,
       extract(second from timestamp '2018-11-12 15:36:01') as second
from dual;

select months_between(to_date('2018-11-12', 'yyyy-mm-dd'),
                      to_date('2017-11-12', 'yyyy-mm-dd')) as zs, --整数
       months_between(to_date('2018-11-12', 'yyyy-mm-dd'),
                      to_date('2017-10-11', 'yyyy-mm-dd')) as xs, --小数
       months_between(to_date('2017-11-12', 'yyyy-mm-dd'),
                      to_date('2018-10-12', 'yyyy-mm-dd')) as fs --负数
from dual;

select sysdate, --当前时间
       round(sysdate, 'yyyy') as year, --按年
       round(sysdate, 'mm') as month, --按月
       round(sysdate, 'dd') as day, --按天
       round(sysdate) as mr_day, --默认不填按天
       round(sysdate, 'hh24') as hour --按小时
from dual;

select sysdate, --当前时间
       trunc(sysdate, 'yyyy') as year, --按年
       trunc(sysdate, 'mm') as month, --按月
       trunc(sysdate, 'dd') as day, --按天
       trunc(sysdate) as mr_day, --默认不填按天
       trunc(sysdate, 'hh24') as hour --按小时
from dual;

数值函数

函数 解释
abs(x) 求数值x的绝对值
cos(x) 求数值x的余弦
acos(x) 求数值x的反余弦
ceil(x) 向上取整
floor(x) 向下取整
log(x,y) 求x为底y的对数
mod(x,y) 求x除以y的余数
power(x,y) 求x的y次幂
sqrt(x) 求x的平方根
round(x[,y]) 求数值x在y位进行四舍五入。y默认为0,可以为负数
trunc(x[,y]) 求数值x在y位进行直接截取。y默认为0,可以为负数

转换函数

函数 解释
asciistr(x) 把字符串转换为数据库字符集对应的ascii值
bin_to_num(x1[x2...]) 二进制数值转换为十进制数值
cast(x as type) 把x转换为type类型的数据
convert(x,d_chset[,r_chset]) 对字符串x按原字符集r_chset转换为目标字符集d_chset
to_char(x[,f]) 把字符串或时间类型x按格式f转换为字符串
to_date(x[,f]) 把字符串x按照格式f转换为时间类型
to_number(x[,f]) 把字符串x按照格式f转换为数值类型

其中数值的格式f可以参考下表:

参数 示例 说明
9 999 指定位置返回数字
. 99.9 指定小数点的位置
99,9 指定位置返回一个逗号
$ $99.9 指定开头返回一个美元符号
eeee 9.99eeee 指定科学计数法

聚合函数

函数 解释
avg([distinct ] expr) 求列或列组成的表达式expr的平均值
count(*|[distinct]expr) 计算查询结果的条数或行数
max([distinct] expr) 指定列或列组成的表达式expr中的最大值
min([distinct] expr) 指定列或列组成的表达式expr中的最小值
sum([distinct] expr) 对指定列或列组成的表达式expr进行求和
select classno, avg(t.age) from stuinfo t where t.age < 30 group by t.classno;

select classno, sum(age), count(*), sum(age) / count(*), avg(age) from stuinfo t where t.age < 30 group by t.classno;

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网