Oracle Sql优化之日期的处理
1.时,分,秒,年,月,日等日期的常用取值方法
select hiredate,
to_number(to_char(hiredate,'hh24')) 时,
to_number(to_char(hiredate,'mi')) 分,
to_number(to_char(hiredate,'ss')) 秒,
to_number(to_char(hiredate,'dd')) 日,
to_number(to_char(hiredate,'mm')) 月,
to_number(to_char(hiredate,'yyyy')) 年,
to_number(to_char(hiredate,'ddd')) 年内第几天,
trunc(hiredate,'dd') 一天的开始,
trunc(hiredate,'day') 周初,
trunc(hiredate,'mm') 月初,
last_day(hiredate) 月末,
add_months( trunc(hiredate,'mm'),1) 下月初,
trunc(hiredate,'yy') 年初,
to_char(hiredate,'day') 周几,
to_char(hiredate,'month') 月份
from emp
2.INTERVAL:INTERVAL类型中保存的是时间间隔信息,可以通过对应的INTERVAL 函数得到INTERVAL类型的数据。
select INTERVAL '' year as "year",
INTERVAL '' month as "month",
INTERVAL '' day as "day",
INTERVAL '' hour as "hour",
INTERVAL '' minute as "minute",
INTERVAL '3.15' second as "second",
INTERVAL '2 12:30:59' DAY to second as "DAY to second",
INTERVAL '13-3' year to month as "year to month"
from dual;
3.EXTRACT:与TO_CHAR 一样,EXTRACT可以提取时间字段中的年月日时分秒,不同的是EXTRACT返回的是NUMBER类型,但是EXTRACT不能提取DATE类型中国的时分秒。
select EXTRACT(YEAR from systimestamp) as "year" from dual; select created,EXTRACT(DAY from created) from dba_objects; ----EXTRACT可以提取INTERVAL中的信息,to_char不行,to_char可以获取日期中的时分秒 select EXTRACT(hour from it) as "hour"
from (select INTERVAL '2 12:30:59‘ DAY to second as it from dual);
4.确定某一年是否为闰年,检查二月月末时哪一天
select to_char(last_day(add_months(trunc(hiredate,'y'),1)),'DD') as "日" from emp
5.周的计算,WW和IW的区别:WW算法为每年的一月一日为第一周的开发,date+6为一周接受,IW的算法为星期一至星期日算一周。next_day(date,1||2),1表示下个周末,2 表示下个周一
with x as
(select trunc(sysdate,'YY')+(level-1) as 日期 from dual connect by level<=8)
select 日期, to_char(日期,'d') as d,to_char(日期,'day') as day,next_day(日期,1),
to_char(日期,'ww') as ww, to_char(日期,'iw') as iw from x;
6.本月日历,枚举指定月份所有的日期,转到对应的周信息。
with x1 as
(select to_date('2013-06-01','yyyy-mm-dd') as curdate from dual),
x2 as
(select trunc(curdate,'mm') as bm, add_months(trunc(curdate,'mm'),1) as bnm from x1),
x3 as
(select bm+(level-1) as day from x2 connect by level<=(bnm-bm)),
x4 as
(select to_char(day,'iw') as inWeek,to_char(day,'dd') dd,to_number(to_char(day,'d')) nweek from x3)
select max(case nweek when 2 then dd end) Mon,
max(case nweek when 3 then dd end) Feb,
max(case nweek when 4 then dd end) Wen,
max(case nweek when 5 then dd end) Thu,
max(case nweek when 6 then dd end) Fri,
max(case nweek when 7 then dd end) Sat,
max(case nweek when 1 then dd end) Sun
from x4
group by inWeek
order by inWeek;
7.Oracle的两个分析函数Lag和lead,分别用户访问结果集中的前一行和后一行
select empno,ename,proj_id,proj_start,
lag(proj_end) over (partition by empno order by proj_start) as lastfinishedproj,
proj_end;
lag(proj_id) over(partition by empno order by proj_start) as lastprojno
from emp
Oracle Sql优化之日期的处理的更多相关文章
- Oracle SQL优化[转]
Oracle SQL优化 1. 选用适合的ORACLE优化器 ORACLE的优化器共有3种: a. RULE (基于规则) b. COST (基于成本) c. CHOOSE (选择性) 设置缺省的优化 ...
- oracle sql 优化大全
转自: http://panshaobinsb.iteye.com/blog/1718233 http://yulimeander.blog.sohu.com/115850824.html 最近遇到了 ...
- Oracle SQL优化原则
原文:http://bbs.landingbj.com/t-0-240353-1.html 1.选用适合的 ORACLE 优化器 2.访问 Table 的方式 3.共享SQL语句 共享的语句必须满足三 ...
- Oracle SQL优化进阶学习
引言 对于下面的Oracle分页如何优化该段语句: SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM task_log order by ...
- 【重磅干货】看了此文,Oracle SQL优化文章不必再看!
目录 SQL优化的本质 SQL优化Road Map 2.1 制定SQL优化目标 2.2 检查执行计划 2.3 检查统计信息 2.4 检查高效访问结构 2.5 检查影响优化器的参数 2.6 SQL语句编 ...
- Oracle SQL 优化原则(实用篇)
由于SQL优化优化起来比较复杂,并且还受环境限制,在开发过程中,写SQL必须遵循以下几点原则: 1.Oracle 采用自下而上的顺序解析WHERE子句,根据这个原理,表之间的连接必须写在其他Where ...
- oracle sql优化
整理一下网上所看到sql优化方法 1.使用大写字母书写sql,因为oracle解释器会先将sql语句转换成大写后再解释 2 减少访问数据库的次数,多数情况下一条sql可以达到目的的,就不要使用多 ...
- Oracle SQL优化一(常见方法)
1.表访问方式优化: a)普通表优先“Index Lookup 索引扫描”,避免全表扫描 大多数场景下,通过“Index Lookup 索引扫描”要比“Full Table Scan (FTS) 全表 ...
- oracle sql优化笔记
oracle优化一般分为:1.sql优化(现在oracle都会根据sql语句先进行必要的优化处理,这种应该用户不大了,但是像关联和嵌套查询肯定是和影响性能的) A.oracle的sql语句的条件是从右 ...
随机推荐
- setting up a IPSEC/L2TP vpn on CentOS 6 or Red Hat Enterprise Linux 6 or Scientific Linux
This is a guide on setting up a IPSEC/L2TP vpn on CentOS 6 or Red Hat Enterprise Linux 6 or Scientif ...
- Time complexity of ArrayList in Java
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add ope ...
- kali linux 更新软件源,安装中文输入法,修复Linux与windows引导菜单解决windows引导丢失
1. 更新软件源打开sources.list文件,进行添加更新源:leafpad /etc/apt/sources.list 2. 添加软件源#官方源 deb http://http.kali.org ...
- linux下ClamAV使用
第一步:Clamav下载http://www.clamav.net/downloads#yuminstall wget –y第二步:创建clamav用户和组groupaddclamav (创建cl ...
- KVM 命令行启动第一台虚拟机
KVM创建第一台虚拟机 1 创建一个镜像 [root@kvm ~]# qemu-img create -f raw /opt/CentOS6.-x86_64.raw 5G Formatting [ro ...
- List泛型集合常用方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...
- #ifdef,#else,#endif,#if 拾忆
预处理就是在进行编译的第一遍词法扫描和语法分析之前所作的工作.说白了,就是对源文件进行编译前,先对预处理部分进行处理,然后对处理后的代码进行编译.这样做的好处是,经过处理后的代码,将会变的很精短. ...
- C/C++宏定义中#与##区别 .
// #表示:对应变量字符串化// ##表示:把宏参数名与宏定义代码序列中的标识符连接在一起,形成一个新的标识符 #define U_BOOT_CMD_MKENT_COMPLETE(name,maxa ...
- CFNetwork SSLHandshake failed (-9824) ios 9
设置 NSAppTransportSecurity
- The Accomodation of Students(判断二分图以及求二分图最大匹配)
The Accomodation of Students Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...