Oracle Database ---- function

- Case Control Function
--upper
SELECT * from EMP WHERE Job = Upper ( 'Salesman');

--lower
select * from emp where lower(job) = 'clerk';

--initcap
select empno, initcap(ename) from emp;

--upper, lower, initcap common of these three functions, if the input parameter is null, then return null
SELECT EMPNO, INITCAP (null) from EMP;

- Character Control function

- string connector, to achieve the connection with the two names of the employees wages
select ename || ':' || sal from emp;

- function strings connected concat, to achieve the employee name and salary of connecting two
select concat (concat (ename, ' :'), sal) from emp;

- cut the string function substr
SELECT * from EMP WHERE substr (Job, l, 4) = 'the SALE';

- seeking function of the length of the string length
SELECT * from EMP WHERE length (ename) =. 6;

--instr Qiuzi position in a string
select instr ( 'hello oracle', 'oracle') from dual;

select instr('hello oracle hello oracle', 'oracle', 1, 2) from dual;

- left padding function LPAD
SELECT LPAD (Job,. 9, '*') from EMP;

- right padding function RPAD
SELECT RPAD (Job,. 9, '*') from EMP;

--替换函数 replace
select replace('hello oracle','oracle','world') from dual;

- rounding function round

- seek daily wage employees
select (sal / 30,2) from emp round;


- truncation function the trunc
SELECT round (SAL / 30,2), the trunc (SAL / 30,2) from EMP;


- seeking remainder of the function Mod

- seeking employee number is an even number of employee information
select * from emp where mod (empno , 2) = 0;

--sysdate
the SELECT sysdate-1 yesterday, sysdate today, sysdate + 1 tomorrow from dual;

--months_between
select round(months_between(sysdate,hiredate)/12)from emp;

--add_months
select ename,add_months(hiredate,30*12) from emp;

--next_day
select next_day(sysdate,'星期一')from dual;

--last_day
select sysdate,last_day(sysdate) from dual;
select empno,ename,last_day(hiredate) from emp;

--round
select hiredate, round(hiredate,'YEAR'), round(hiredate,'MONTH') from emp where empno=7654;

--trunc
select hiredate, trunc(hiredate,'YEAR'), trunc(hiredate,'MONTH') from emp where empno=7654;

- implicit data type conversions example
SELECT * from EMP WHERE SAL> '2000';
SELECT * from EMP WHERE HireDate = '02 - April -81 ';

Conversion --to_char date type to a character type of
the SELECT from the TO_CHAR emp (the HireDate, 'YYYY-MM-DD');
the SELECT the TO_CHAR (the HireDate, 'YYYY "in" MM "month" DD "Day"') from emp;
the SELECT the TO_CHAR (hiredate, 'DD-MON- RR', 'NLS_DATE_LANGUAGE = AMERICAN') from emp;

--to_char converted into numeric type character type
SELECT SAL, TO_CHAR (SAL, 'L0,000,000.00') from EMP;
SELECT SAL, TO_CHAR (SAL, 'L9,999,999.99') from EMP;
SELECT SAL, TO_CHAR (SAL, '$ 9,999,999.99 ') from emp;

--to_date converted to date type character type
select ename, hiredate from emp where hiredate > to_date ( '1981-12-31', 'YYYY-MM-DD');

--to_number converted into numeric type character type
select ename, sal from emp where sal > to_number ( '¥ 2000', 'L99999');

--nvl
select ename,sal,comm,sal+nvl(comm,0) from emp;

--nvl2
select ename,sal,comm,nvl2(comm,comm+sal,sal) from emp;

--nullif
select empno,ename,hiredate,nullif(hiredate,trunc(sysdate,'MONTH'))from emp;

--coalesce
select ename,sal,comm,coalesce(sal+comm,sal) from emp;

- Employees want to display all jobs, but these jobs requires replaced by Chinese show:
-
--CLERK: clerks;
--SALESMAN: Sales;
--MANAGER: Manager;
--ANALYST: Analyst;
--PRESIDENT: President ;

--case expression of
the SELECT empno, ename,
Case the Job
the when 'CLERK' the then 'Clerks'
the when 'SALESMAN' the then 'sell'
the when 'MANAGER' the then 'manager'
the when 'ANALYST' the then 'analyst'
the else 'president'
End
from emp;

--decode function
select empno, ename, job, decode (job, 'CLERK', ' Clerks',' SALESMAN ',' sales', 'MANAGER', 'manager', 'ANALYST', 'analysts',' president ') from emp;

--case expression value determination section
SELECT EMPNO, ename, SAL,
Case When SAL <2000 the then 'low'
When SAL <5000 the then 'in'
the else 'high'
End
from EMP;

- referring to employee information table, I want to show the next Monday from the date three months after the date of hire, and date formats such as: 2017-01-06.

select empno,ename,to_char(next_day(add_months(hiredate,3),'星期一'),'YYYY-MM-DD') new_date
from emp;

- Referring employee information table, display employee daily rate and rounded to two decimal places of the result, then the pay format formatted '¥ 1,182.19' such examples in the form of

select empno,ename,sal,to_char(round(sal/30,2),'L9,999.99')
from emp;

- averaging AVG
SELECT AVG (SAL) from EMP;

--求和sum
select sum(sal) from emp;

- selecting the maximum value max
SELECT max (SAL) from EMP;

- minimization min
SELECT min (SAL) from EMP;

- counting functions COUNT
SELECT COUNT (*) from EMP;
SELECT COUNT (EMPNO) from EMP;

- statistics about the number of sectors
the SELECT COUNT (deptno) from emp;
- the grouping function to remove duplicate records by distinct keyword
select count (distinct deptno) from emp ;

--行转列wm_concat
select wm_concat(ename) from emp;

- averaging wages of employees
select sum (sal) / count ( *) method, sum (sal) / count ( sal) Method II, avg (sal) Method three emp from;
- averaging grants employees
select sum (comm) / count (*) method a, sum (comm) / count ( comm) method II, avg (comm) method three from emp;

select count(*),count(nvl(comm,0)) from emp;

COMM from EMP SELECT;
- is not Null values are ignored
SELECT AVG (NVL (COMM, 0)) from EMP;
- Ignore blank
select avg (comm) from emp;

- finds the average salary for each department, required to display: department number, average wage sector
select deptno, avg (sal) from emp group by deptno;

- Multi-column grouping
- by different departments, different positions, calculating the average wage of employees
select deptno, job, avg (sal ) from emp group by deptno, job order by deptno;

- finds the average salary for each department, required to display: the average salary for each department.
select avg (sal) from emp group by deptno;

- find the names of employees in each department, required to display: department number, employee name
select deptno, wm_concat (ename) from emp group by deptno;

- averaging greater than 2500 wage sector, required to display: department number, average wage

--select deptno,avg(sal) from emp where avg(sal)>2500 group by deptno;

- if the condition included in the packet function, the packet needs to be filtered results having clauses
select deptno, avg (sal) from emp group by deptno having avg (sal)> 2500;

- If there is no packet function, recommended for use in conditions where clause, the packet can be filtered to improve the efficiency
select deptno, avg (sal) from emp where deptno = 10 group by deptno;

- sector seeking maximum average wage
- nested grouping functions to be used with group by clause
select max (avg (sal)) from emp group by deptno;

 

Guess you like

Origin www.cnblogs.com/xiaomifeng1010/p/11111207.html