--返回日期的后一天
select to_char(sysdate,'yyyy.mm.dd'),to_char((sysdate)+1,'yyyy.mm.dd') from dual;
--返回日期的最后一天
select last_day(sysdate) from dual;
--给出date2-date1的月份
select months_between('15-08月-2020','10-05月-1991') mon_between from dual;
select months_between(to_date('2021.05.10','yyyy.mm.dd'),to_date('2021.08.10','yyyy.mm.dd')) mon_betw from dual;
--给出日期date和星期x之后计算下一个星期的日期
0-6:代表星期日-星期六
select next_day(to_date('2021.06.09','yyyy.mm.dd'),2) next_day from dual;
select next_day(to_date('2021.06.09','yyyy.mm.dd'),5) FROM DUAL;
--SYSDATE 用来得到系统的当前日期
select to_char(sysdate,'dd-mm-yyyy day') from dual;
--trunc(date,fmt)按照给出的要求将日期截断,如果fmt='mi'表示保留分,截断秒
select to_char(trunc(sysdate,'hh'),'yyyy.mm.dd hh24:mi:ss') hh, to_char(trunc(sysdate,'mi'),'yyyy.mm.dd hh24:mi:ss') hhmm from dual;
--date转字符串
select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') from dual;
--TO_DATE(string,'format') 将字符串转化为ORACLE中的一个日期
select to_date('2005.05.20','yyyy.mm.dd') from dual;
--将给出的字符转换为数字
select to_number('1999') year from dual;
--.RPAD和LPAD(粘贴字符)
select lpad(rpad('gao',10,'*'),17,'*')from dual;
--LTRIM和RTRIM LTRIM 删除左边出现的字符串 RTRIM 删除右边出现的字符串
select ltrim(rtrim(' gao qian jing ',' '),' ') from dual;
--SUBSTR(string,start,count) 取子字符串,从start开始,取count个
select substr('13088888888',3,8) from dual;
--REPLACE('string','s1','s2') string :望被替换的字符或变量,s1: 被替换的字符串 , s2: 要替换的字符串
select replace('he love you','he','i') from dual;
-- 分页
SELECT
*
FROM(
SELECT
a.*,
ROW_NUMBER() OVER(ORDER by empno DESC) rk FROM emp a )
WHERE rk<10 and rk >= 1;
SELECT
*
FROM(
SELECT
t.*,
rownum rn
FROM
(
SELECT
* FROM emp ORDER by empno DESC)t WHERE rownum<10)
WHERE rn >= 1;
-- 递归查询(用于树结构查询)
select t.id,LENGTHB(name),
4*(LEVEL-1),
Lpad(t.name,LENGTHB(name)+4*(LEVEL-1)),
RPAD(' ',4*(LEVEL-1))||name,
t.pid,
LEVEL from BIANLI t START WITH pid='-1' CONNECT BY PRIOR id=pid
--表所有记录
-- 查找每个部门工资最高前三名员工信息
SELECT
*
FROM ( SELECT deptno,
ename,
sal,
DENSE_RANK()OVER(PARTITION by deptno ORDER by sal DESC ) a FROM emp )
WHERE a <= 3 ORDER BY deptno ASC,
sal DESC;
SELECT
*
FROM(SELECT deptno,
ename,
sal,
RANK()OVER(PARTITION by deptno ORDER by sal DESC) b FROM emp )
WHERE b <= 3 ORDER BY deptno ASC,
sal DESC;