关于Oracle和PL/SQL的学习记录4

------数据查询中函数的使用------
1.count函数-聚合函数
select count(sdept) from stu where sdept like '12计算机';
select count(*) from stu where sdept = '12计算机';
count函数通常与关键字distinct一起使用,关键字distinct用于消除重复记录,与关键字dsitinct一起使用可以统计表中不重复的记录个数
关键字distinct应该写在count()函数里面,并就其中不重复的列进行统计,不能使用*,否则会报错

2.sum函数-聚合函数
select sum(salary) from Salary;
sum函数只能对数值数据类型的列求和;
sum函数也支持distinct关键字,如果使用distinct关键字,则统计汇总的总工资是消除了重复记录的数目;

3.avg函数-聚合函数
select avg(salary) from Salary;
avg函数只允许对数值型数据进行操作

4.max函数-聚合函数
select max(sage) 男生最大年龄 from stu where sgender = '男';
max函数可以用于非数值的数据中:
select max(sname) from stu;
系统返回的是姓名sname字段的字符串最大值;在对中文汉字进行比较时,Oracle将比较其拼音字母

5.min函数-聚合函数
select min(sage) 女生最小年龄 from stu where sgender='女';

以上聚合函数不能在where子句中使用;

6.replace函数
查找并替换字符串
select replace(sdept,'计算机','compute') 所在班级 from stu;
在select语句中使用replace函数并不会改变数据表的字段值,只是改变其在显示在控制端的值,表中的字段是不会被修改的

7.ascll函数-获取字符的ascll码
select ascll('A') A,ascll('Z') Z,ascll('0') zero,ascll(' ') space from dual;
dual表是Oracle中对所有用户可用的一个实际存在的表,这个表不能用来存储信息,在实际应用中仅用来执行select语句
dual表是一个一行一列的表,其结构已固定,用户不能向该表进行插入、删除等操作

8.length函数
select sname,length(sname),sdept,length(sdept) from stu where sno = '10001';
length函数不仅可以统计字符串的长度,还能对数值统计位数

9.round函数-确定精度
select round(avg(sage),3) 平均年龄 from stu;
设置平均年龄输出精度为小数点后三位,在实际输出时采用四舍五入来实现
当round函数中取出的位数为负数时,表示对小数点前的数字进行四舍五入操作

10.add_months函数
select sno,sname,add_months(sbirth,12) from stu;
add_months(d,n):参数d表示日期,参数n表示要加的月数,函数表示在某一个日期d上,加上指定的月数n,返回计算后的新日期

11.to_char()-日期格式化输出
select to_char(sysdate,'yyyy/mm/dd') from dual;
select to_char(sysdate,'yyyy/mm/dd hh:mi:ss') from dual;
select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') from dual;
select to_char(sysdate,'yyyy"年"mm"月"dd"日"') from dual;
在自定义格式输出时,格式化字符串中,使用双引号对非格式化字符进行引用

12.extract函数-实现特定日期时间的提取
select sysdate 当前日期时间,extract(year from sysdate) 年份,extract(month from sysdate) 月份,extract(day from sysdate) 日,extract(hour from systimestamp) 小时,extract(minute from systimestamp) 分,extract(second from systimestamp) 秒 from dual;
hour、minute、second必须与timestamp类型匹配;

13.数据类型转换
to_char()
to_number()
to_date()

14.提取日期的星期值:
select to_char(sysdate,'day') from dual;

提取一年中的第几天:
select to_char(sysdate,'ddd') from dual;
select to_char(to_date('2019-10-24','yyyy-mm-dd'),'ddd') from dual;

计算时间差:
select floor(to_number(sysdate-to_date('2019-10-23','yyyy-mm-dd'))) as spandays from dual;

获取某月的第一天与最后一天:
select trunc(trunc(sysdate,'month')-1,'month') first_day_last_month,trunc(sysdate,'month')-1/86400 last_day_last_month,trunc(sysdate,'month') first_day_cur_month,last_day(trunc(sysdate,'month'))+1-1/86400 last_day_cur_month from dual;
trunc函数用于截取指定数据的部分,并不会对指定小数前或后的部分做相应舍入选择处理,而只是截取

15.nvl函数
select eno,ename,salary,nvl(comm,100) from salary where salary < 3000;
如果comm为空,则返回100;
nvl(x,value):如果x为空,则返回value;
nvl2(x,value1,value2):如果x非空,返回value1,否则返回value2;
select eno,ename,salary,nvl(comm,comm+100,200) 加法奖金 from salary where salary < 3000;
如果nvl函数中的参数x和value的结果都为null值,那么nvl函数最终的返回值为null

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值