1. 程式人生 > >單行函數的案例

單行函數的案例

rim date upper 表示 subst 去除 lower div cas

select * from scott.emp;
select ename "name"from scott.emp;
--lower轉化小寫的用法
select lower(ename) ename ,sal from scott.emp;

select * from scott.emp where lower(ename)=‘allen‘;
--upper 轉化大寫
select upper(ename) "ename" from scott.emp ;
--INITCAP 首字母大寫
select INITCAP(ename) as ename from scott.emp ;
--CONCAT 或 ||連接
select concat(ename,sal )from scott.emp;
select concat (
‘wang‘,‘feng‘) ename from scott.emp; select ename||‘ ‘||sal ename from scott.emp; select [email protected][email protected]as ename from scott.emp; --SUBSTR select SUBSTR(ename ,1,3)from scott.emp; select substr(hiredate , 0)from scott.emp; --尋找月份 select* from scott.emp where hiredate = ‘22-2月-81‘; select
* from scott.emp where hiredate > ‘1-1月-81‘; select * from scott.emp where hiredate between ‘1-1月-81‘and‘31-12月- 81‘; --LENGTH 長度 select length(ename) as chang from scott.emp; --INSTR返回第一次出現某字符的位置 select INSTR(ename,‘L‘)from scott.emp; --LPAd 變成一個長度為10的字符,不夠用*代替 select lpad (ename ,10,‘*‘)FROM scott.emp;
--RPAD 變成一個長度為10的字符串,不夠在其後面加* select rpad (ename ,10,‘*‘)from scott.emp; select rpad (ename ,3)from scott.emp; --TRIM 去除字符兩端的某個字符 select trim (‘C‘from ename)from scott.emp; select ‘[‘||‘ ‘||ename||‘ ‘||‘]‘from scott.emp; select trim (ename)from (select ‘ [‘||‘ ‘||ename||‘ ‘||‘] ‘ ename from scott.emp); select trim (‘a‘from ‘aaabbbbaa‘) from scott.emp; --"Contains ‘a‘?" --select from scott.emp; --ROUND: 四舍五入 select round(7850.3854512145,5)from scott.emp; select round(7850.38,5)from scott.emp; select round(7850.38,-2)from scott.emp; --TRUNC: 截斷 select trunc (222222.2222,2)from scott.emp ; select trunc (222222,-2)from scott.emp ; --日期報錯select trunc (hiredate)from scott.emp; select trunc (222222.222,0)from scott.emp ; --MOD: 求余 select mod (3,2)from scott.emp; --sysdate 返回時間毫秒表示 select sysdate-hiredate from scott.emp; --substr負數從後面算起 Select substr(‘abcde‘,-4,3) from scott.emp; --replace select replace (ename,‘A‘,‘a‘)from scott.emp; --Months_between() select months_between(sysdate,add_months(sysdate,-2)) from scott.emp; select sysdate from scott.emp;--返回現在時間 --add_months select add_months(sysdate,1) from scott.emp; --select add_days(sysdate,2)from scott.emp; add-years (),add_day()無效 --next_day select next_day(sysdate,‘星期一‘)from scott.emp; --order by select * from scott.emp order by sal,empno asc; select * from scott.emp order by sal,empno desc; --Last_day select last_day (sysdate)from scott.emp; select add_months (sysdate,-2) from scott.emp; --ROUND 日期 select round (sysdate,‘year‘)from scott.emp; select round (sysdate ,‘month‘)from scott.emp; select round (sysdate,‘day‘)from scott.emp; --trunc截取日期 select trunc (sysdate,‘year‘)from scott.emp; select trunc (sysdate,‘month‘)from scott.emp; select trunc (sysdate,‘day‘)from dual; --轉化函數 -- to_char()對於日期的轉化 select to_char (sysdate,‘yyyy‘)from scott.emp; select to_char (hiredate,‘yy‘)from scott.emp; select to_char (sysdate ,‘yyyy-mm-dd‘)from scott.emp; select to_char (hiredate, ‘mm‘)from scott.emp; --生日在5到8月之間 select *from scott.emp where to_number(to_char (hiredate, ‘mm‘))between 7 and 9; select *from scott.emp where to_number(to_char (hiredate, ‘mmdd‘))between to_number(to_char (sysdate, ‘mmdd‘)) and to_number(to_char (add_months(sysdate,2), ‘mmdd‘)); select to_number(to_char (add_months(sysdate,2), ‘mm‘)) from dual; select to_char(sysdate,‘fmyyyy-mm-dd‘) from scott.emp; select to_char (hiredate,‘fmyyyy-mm‘) from scott.emp; select to_char (sysdate,‘fmyyyy-mm-dd-hh24-mi-ss‘)from scott.emp; select to_char (sysdate,‘fmyyyy/mm/dd/hh/mi/ss‘)from scott.emp; select to_char(sal,‘l999,999‘) from scott.emp; select to_char(sal,‘$999,999‘) from scott.emp; select to_char (sysdate,‘d‘)from Scott.emp; --To_number把字符串變成數字 select to_number(‘13‘)+to_number(‘14‘) from dual; --to_date select to_date (‘20090222‘,‘yyyymmdd‘)from dual; --NVL()函數 select nvl(comm,0) from scott.emp; select * from scott.emp; --nvl2(comm, sal+comm, sal) select nvl2(comm, sal+comm, sal) from scott.emp; --COALESCE()函數 select empno, ename, sal, comm, coalesce(sal+comm, sal, 0)as sal from scott.emp; --case select empno, ename, sal, case deptno when 10 then ‘財務部‘ when 20 then ‘研發部‘ when 30 then ‘銷售部‘ else ‘未知部門‘ end from scott.emp; --DECODE()函數 select empno, ename, sal, decode(deptno, 10, ‘財務部‘, 20, ‘研發部‘, 30, ‘銷售部‘, ‘未知 部門‘) 部門 from scott.emp; select * from scott.emp where job != ‘CLERK‘; select * from scott.emp order by sal desc ; --查詢出每個部門的編號、名稱、位置、部門人數、平均工資 select * from scott.emp where deptno = 10;

單行函數的案例