show-sales.com

awed

 

oracle学习笔记

2008-4-17上午

练习中所使用的表全为ORACLE安装时所表的数据及表。

 

-- distinct 去掉重复的记录
  select distinct deptno,ename,sal from emp ;
  
  
--substr(str,start,len),截取字符串,STR需要截取的字符串或列,START为从第几个字符开始,LEN截取多长
  select substr(ENAME,2,2from emp order by deptno asc,ename desc;
  
  
select chr(65from dual; --将一个数转换为字符
  
  
select ascii('A'from dual;--求一个数的ASCII码
  
  
select round(23.652from dual ;--四舍五入
  
  
select round(23.45902234,2from dual;--四舍五入,后点小数2位
  
  
select to_char(sal,'$99,999.9999'from emp ;
  
--将一个数转换为字符串并按某种格式,
  --其中一个9代表一个数字,如果不够位数取后面位,


  
select to_char(sal,'L99,999.9999'from emp ;--前面加上L,即Local加上本地字符串
  
  
select to_char(hiredate,'yyyy-mm-dd HH:mm:ss'from emp;
  
  
select to_char(sysdate,'yyyy-mm-dd hh24:mm:ss') today from dual ;
  
  
  
--日期转换函数to_date(str1,str2) str1需要转换的字符串,str2为转换成什么格式
  select * from emp where hiredate > to_date('1981-02-01','yyyy-mm-dd')
  
  
--将字符串转换为数字to_number(str1,str2)str1需要转换的字符串,str2为转换成什么格式
  select sal from emp where sal > to_number('$1,220.00','$99,999.9999')
  
  
--NULL情况处理,使用nvl(str1,str2),str1为需要处理的列,STR2为为空时默认的值,如果为空时则为0,不为NULL时则直接为comm
  select ename,nvl(comm,0) comm from emp ;
  
  
--四入五入到几位,
  select round(max(sal),2) 最大工资,round(min(sal),2) 最小工资,round(avg(sal),2) 平均工资 from emp ;
  
  
--将数字转换为某种格式的字符串
  select to_char(max(sal),'L9,999.99') 最大工资,to_char(min(sal),'L9,999.99') 最小工资,to_char(avg(sal),'L9,999.99') 平均工资 from emp ;
  
  
--group by分组查询
  select sal,deptno from emp group by deptno,sal;
  
  
--求所有员工中单个部门工资最高的员工所有信息
  select A.* from emp A
  
inner join
  (
select deptno, max(sal) as total from emp group by deptno) B
  
on A.Deptno=B.deptno and A.Sal=B.total

posted on 2008-04-17 10:33 一鸣 阅读(1020) 评论(0)  编辑  收藏 所属分类: SQL


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告


常用链接

留言簿(3)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜