TO_DATE格式
Day:
dd number 12
dy abbreviated fri
day spelled out friday
ddspth spelled out, ordinal twelfth

Month:
mm number 03
mon abbreviated mar
month spelled out march

Year:
yy two digits 98
yyyy four digits 1998
24小时格式下时间范围为: 0:00:00 - 23:59:59.... 12小时格式下时间范围为: 1:00:00 - 12:59:59 ....

Y或YY或YYY 年的最后一位,两位或三位

Select to_char(sysdate,’YYY’) from dual;002表示2002年

SYEAR或YEAR SYEAR使公元前的年份前加一负号

Select to_char(sysdate,’SYEAR’) from dual; -1112表示公元前111 2年

Q 季度,1~3月为第一季度 Select to_char(sysdate,’Q’) from dual; 2表示第二季度①

MM 月份数 Select to_char(sysdate,’MM’) from dual; 12表示12月

RM 月份的罗马表示 Select to_char(sysdate,’RM’) from dual; IV表示4月

Month 用9个字符长度表示的月份名 Select to_char(sysdate,’Month’) from dual; May后跟6个空格表示5月

WW 当年第几周 Select to_char(sysdate,’WW’) from dual; 24表示2002年6月13日为第24周

W 本月第几周 Select to_char(sysdate,’W’) from dual; 2002年10月1日为第1周

DDD 当年第几, 1月1日为001,2月1日为032 Select to_char(sysdate,’DDD’) from dual; 363 2002年1 2月2 9日为第363天

DD 当月第几天 Select to_char(sysdate,’DD’) from dual; 04 10月4日为第4天

D 周内第几天 Select to_char(sysdate,’D’) from dual; 5 2002年3月14日为星期一

DY 周内第几天缩写 Select to_char(sysdate,’DY’) from dual; SUN 2002年3月24日为星期天

HH或HH12 12进制小时数 Select to_char(sysdate,’HH’) from dual;02 午夜2点过8分为02 HH24 24小时制 Select to_char(sysdate,’HH24’) from dual; 14 下午2点08分为14

MI 分钟数(0~59) Select to_char(sysdate,’MI’) from dual; 17下午4点17分

SS 秒数(0~59) Select to_char(sysdate,’SS’) from dual; 22 11点3分22秒 提示注意不要将MM格式用于分钟(分钟应该使用MI)。

MM是用于月份的格式,将它用于分钟也能工作,但结果是错误的

现在给出一些实践后的用法:

1。上月末天: SQL> select to_char(add_months(last_day(sysdate),-1),'yyyy-MM-dd') LastDay from dual;

LASTDAY ---------- 2005-05-31

2。上月今天 SQL> select to_char(add_months(sysdate,-1),'yyyy-MM-dd') PreToday from dual;

PRETODAY ---------- 2005-05-21

3.上月首天 SQL> select to_char(add_months(last_day(sysdate)+1,-2),'yyyy-MM-dd') firstDay from dual;

FIRSTDAY ---------- 2005-05-01

4.按照每周进行统计 SQL> select to_char(sysdate,'ww') from dual group by to_char(sysdate,'ww');

TO -- 25

5。按照每月进行统计 SQL> select to_char(sysdate,'mm') from dual group by to_char(sysdate,'mm');

TO -- 06

6。按照每季度进行统计 SQL> select to_char(sysdate,'q') from dual group by to_char(sysdate,'q');

T - 2

7。按照每年进行统计 SQL> select to_char(sysdate,'yyyy') from dual group by to_char(sysdate,'yyyy');

TO_C ---- 2005

8.要找到某月中所有周五的具体日期 select to_char(t.d,'YY-MM-DD') from ( select trunc(sysdate, 'MM')+rownum-1 as d from dba_objects where rownum < 32) t where to_char(t.d, 'MM') = to_char(sysdate, 'MM') --找出当前月份的周五的日期

and trim(to_char(t.d, 'Day')) = '星期五' -------- 03-05-02 03-05-09 03-05-16 03-05-23 03-05-30

如果把where to_char(t.d, 'MM') = to_char(sysdate, 'MM')改成sysdate-90,即为查找当前月份的前三个月中的每周五的日期。

9.oracle中时间运算

内容如下: 1、oracle支持对日期进行运算 2、日期运算时是以天为单位进行的 3、当需要以分秒等更小的单位算值时,按时间进制进行转换即可 4、进行时间进制转换时注意加括号,否则会出问题

SQL> alter session set nls_date_format='yyyy-mm-dd hh:mi:ss';
会话已更改。
SQL> set serverout on SQL>
declare 
DateValue date; 
begin 
select sysdate into DateValue from dual; 
dbms_output.put_line('源时间:'||to_char(DateValue)); 
dbms_output.put_line('源时间减1天:'||to_char(DateValue-1)); 
dbms_output.put_line('源时间减1天1小时:'||to_char(DateValue-1-1/24)); 
dbms_output.put_line('源时间减1天1小时1分:'||to_char(DateValue-1-1/24-1 /(24*60))); 
dbms_output.put_line('源时间减1天1小时1分1秒:'||to_char(DateValue-1-1/24-1 /(24*60)-1/(24*60*60))); 
end; 
/ 源时间:2003-12-29 11:53:41 源时间减1天:2003-12-28 11:53:41 源时间减1天1小时:2003-12-28 10:53:41 源时间减1天1小时1分:2003-12-28 10:52:41 源时间减1天1小时1分1秒:2003-12-28 10:52:40

PL/SQL 过程已成功完成。

在Oracle中实现时间相加处理 -- 名称:Add_Times -- 功能:返回d1与NewTime相加以后的结果,实现时间的相加 -- 说明:对于NewTime中的日期不予考虑

create or replace function Add_Times(d1 in date,NewTime in date)
return date is hh   number;
mm   number; ss   number;
hours number; dResult date;
begin -- 下面依次取出时、分、秒
select to_number(to_char(NewTime,'HH24')) into hh from dual;
select to_number(to_char(NewTime,'MI')) into mm from dual;
select to_number(to_char(NewTime,'SS')) into ss from dual; -- 换算出NewTime中小时总和,在一天的百分几
hours := (hh + (mm / 60) + (ss / 3600))/ 24; -- 得出时间相加后的结果
select d1 + hours into dResult from dual; return(dResult);
end Add_Times;

-- 测试用例 -- select Add_Times(sysdate,to_date('2004-12-06 03:23:00','YYYY-MM-DD HH24:MI:SS')) from dual

 
其它方法:
select   extract(month   from   query_cxrq)  from app_query
extract(year from query_cxrq)年度
extract(month from query_cxrq)月份
extract(day from query_cxrq)日
 --转换带时间的日期格式变量为shortdate格式时间字符串方法:
1、datetime.tostring("yyyy.MM.dd")
2、formatdatetime(datetime,"yyyy.MM.dd")
3、convert.todatetime(str).ToShortDateString()

ORACLE时间常用函数(字段取年、月、日、季度)的更多相关文章

  1. Sql日期时间格式转换;取年 月 日,函数:DateName()、DATEPART()

    一.sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007 ...

  2. php数学和时间常用函数有哪些(总结表)(看学习视频效率挺高的)(复习)

    php数学和时间常用函数有哪些(总结表)(看学习视频效率挺高的)(复习) 一.总结 一句话总结: 1.数学函数常用的6个:max().min().cell().floor().round().mt_r ...

  3. Oracle时间日期函数

    ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02   13:45:25为例)           Year:              yy two digits 两位年 ...

  4. MS SQL Server时间常用函数

    SQLServer时间日期函数详解,SQLServer,时间日期, 1.      当前系统日期.时间 select getdate() 2. dateadd      在向指定日期加上一段时间的基础 ...

  5. Oracle数据库常用函数

    Oracle常用函数: 20.COS返回一个给定数字的余弦SQL> select cos(-3.1415927) from dual;COS(-3.1415927)--------------- ...

  6. oracle计算时间常用函数

    --ddd:一年中的第几天 select to_char(sysdate,'ddd') from dual --d:一周中的第几天 星期天是第一天 所以要-1select to_char(sysdat ...

  7. Oracle 之 常用函数

    SQL语句根据参数的不同,分为单行函数 和 多行函数. [1] 单行函数:输入是一行,输出也是一行: [2] 多行函数:输入多行数据,输出一个结果. 在执行时,单行函数是检索一行处理一次,而多行函数是 ...

  8. 【Oracle】常用函数

    来源自:https://www.cnblogs.com/lxl57610/p/7442130.html Oracle SQL 提供了用于执行特定操作的专用函数.这些函数大大增强了 SQL 语言的功能. ...

  9. Oracle开发常用函数与存储过程

    create or replace function Fuc_Get_AuthorName(RecID_In in varchar2, AdmID_In in varchar2) return var ...

随机推荐

  1. antidependence and data hazard

    See below example. ADDD  F6, F0, F8 SUBD   F8, F10, F14 Some article would say that “ There’s an ant ...

  2. where方法的用法是ThinkPHP查询语言的精髓

    where方法的用法是ThinkPHP查询语言的精髓,也是ThinkPHP ORM的重要组成部分和亮点所在,可以完成包括普通查询.表达式查询.快捷查询.区间查询.组合查询在内的查询操作.where方法 ...

  3. python相关软件安装流程图解————————pycharm安装——————pycharm-professional-2018.3.1

    https://www.jetbrains.com/pycharm/download/#section=windows http://www.cnblogs.com/ceshi2016/p/91129 ...

  4. 深入浅出 Java Concurrency (28): 线程池 part 1 简介[转]

    从这一节开始正式进入线程池的部分.其实整个体系已经拖了很长的时间,因此后面的章节会加快速度,甚至只是一个半成品或者简单化,以后有时间的慢慢补充.完善. 其实线程池是并发包里面很重要的一部分,在实际情况 ...

  5. PAT甲级——A1087 All Roads Lead to Rome【30】

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

  6. java基础之BigInteger

    BigInteger类概述可以让超过Integer范围内的数据进行运算 构造方法 public BigInteger(String val) 成员方法: public BigInteger add(B ...

  7. day69test

    目录 前端  vue main.js  vue CarTag.vue 小组件  vue Nav.vue小组件  vue Home.vue 页面  vue Car.vue 页面  vue CarDeta ...

  8. 使用RequestsCookieJar自动保存并传递cookie

    使用python的requests开发爬虫程序的时候,经常需要将之前请求返回的cookie值作为下一个请求的cookie进行调用,比如模拟登录之后的返回的sessionID,就是需要作为后续请求的co ...

  9. redux在react项目中的应用

    今天想跟大家分享一下redux在react项目中的简单使用 1 1.redux使用相关的安装 yarn add redux yarn add react-redux(连接react和redux) 2. ...

  10. 关于CoCreateInstance的0x800401f0问题

    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&g_pGr ...