Note: dual as a virtual table, it has no real meaning, just to complement sql statement
One-way function : acting on the line, a return value
--字符函数,小写变大写
select upper('yes') from dual;
--字符函数,大写变小写
select lower('YES') from dual;
--数值函数
select round(26.18) from dual;
Rounding, the latter parameter represents the number of decimal places retained, or may be negative (that is, to the integer part)
- numerical function
select round (26.18,1) from dual;
--数值函数
select round(26.14,1) from dual;
--直接截取保留一位小数
select trunc(56.16,1) from dual;
--求余数
select mod(10,3) from dual;
Date Functions (direct plus or minus, is derived days)
--日期函数
--查出EMP表中所有员工入职距离现在多少天
select sysdate-e.hiredate from emp e;
--算出明天此刻,可以直接与数字加减
select sysdate+1 from dual;
--查出EMP表中所有员工入职距离现在几月
select months_between(sysdate,e.hiredate) from emp e;
In this function is not so in this way can not be used
--查出EMP表中所有员工入职距离现在几年
select months_between(sysdate,e.hiredate)/12 from emp e;
Fixed for 12 months each year
- detect entry from the EMP table all week how many employees now
select (sysdate-e.hiredate) / 7 from emp e;
--转换函数【日期转字符串】【取出之后是字符串】
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
select to_char(sysdate,'fm yyyy-mm-dd hh:mi:ss') from dual;
select to_char(sysdate,'fm yyyy-mm-dd hh24:mi:ss') from dual;
--字符串转日期【取出之后是日期类型】
select to_date('2019-5-25 17:14:38','fm yyyy-mm-dd hh24:mi:ss') from dual;
General function
--算出EMP表中所有员工的年薪记得加上奖金
--奖金里面有null值,如果null值和任意数字做算数运算,结果都是null
select e.sal*12+nvl(e.comm,0) from emp e;
nvl (e.comm, 0) represents the value is null if e.comm to use later
Multi-line function
EMP table field
--多行函数【聚合函数】:作用域多行,返回一个值
--count(1)就相当于count(empno),此外count(*)底层走的还是count(1)
select count(1) from emp;
select sum(sal) from emp;
select max(sal) from emp;
select min(sal) from emp;
select avg(sal) from emp;
The results are: