2008-4-17上午
練習中所使用的表全為ORACLE安裝時所表的數據及表。
-- distinct 去掉重復的記錄
select distinct deptno,ename,sal from emp ;
--substr(str,start,len),截取字符串,STR需要截取的字符串或列,START為從第幾個字符開始,LEN截取多長
select substr(ENAME,2,2) from emp order by deptno asc,ename desc;
select chr(65) from dual; --將一個數轉換為字符
select ascii('A') from dual;--求一個數的ASCII碼
select round(23.652) from dual ;--四舍五入
select round(23.45902234,2) from 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
