原文地址:Oracle date timestamp 毫秒 - 时间函数总结

yyyy-mm-dd hh24:mi:ss.ff  年-月-日 时:分:秒.毫秒

--上一月,上一年
select add_months(sysdate,-1) last_month,add_months(sysdate,-12) last_year from dual;
--下一月,下一年
select add_months(sysdate,1) last_month,add_months(sysdate,12) last_year from dual;

--当月最后一天

select LAST_DAY(sysdate) from dual;

--下周日期

select next_day(sysdate,'星期五') "下周五" from dual;
select next_day(sysdate, 'Friday') "Next Friday" from dual;
select next_day(sysdate, 4) from dual;
如果你不确定自己的时区或者你担心从一个时区移植到另一个时区时,SQL语句会出错,Oracle还允许你用数字的形式来表示工作日。
但是要记得一点:1表示的是周日,2表示的是周一,3表示的是周二,依此类推。

--固定日期一天
select * from account a where a.applytime>= to_date('2011-02-26','yyyy-mm-dd') and a.applytime< to_date('2011-02-27','yyyy-mm-dd');
--前天一天
select * from account a where a.applytime>= to_date(to_char(sysdate-2,'yyyy-mm-dd'),'yyyy-mm-dd') and a.applytime< to_date(to_char(sysdate-1,'yyyy-mm-dd'),'yyyy-mm-dd');
昨天一天
select * from dxw_tmp a where a.applytime>= to_date(to_char(sysdate-1,'yyyy-mm-dd'),'yyyy-mm-dd') and a.applytime< to_date(to_char(sysdate,'yyyy-mm-dd'),'yyyy-mm-dd');
--今天一天
select * from account a where a.applytime>= to_date(to_char(sysdate,'yyyy-mm-dd'),'yyyy-mm-dd') and a.applytime< to_date(to_char(sysdate+1,'yyyy-mm-dd'),'yyyy-mm-dd');

上月第一天
select to_date(to_char(add_months(sysdate,-2)+1,'yyyy-mm-dd'),'yyyy-mm-dd') from dual ;
上月最后一天
select to_date(to_char(add_months(sysdate,-1),'yyyy-mm-dd'),'yyyy-mm-dd') from dual;

select * from account a where a.applytime>= to_date(to_char(add_months(sysdate,-2)+1,'yyyy-mm-dd'),'yyyy-mm-dd') and a.applytime<= to_date(to_char(add_months(sysdate,-1),'yyyy-mm-dd'),'yyyy-mm-dd');

select to_date(to_char(add_months(sysdate,-2),'yyyy-mm-dd'),'yyyy-mm-dd') from dual;
select to_date(to_char(add_months(sysdate,-3)+1,'yyyy-mm-dd'),'yyyy-mm-dd') from dual ;

--trunc 截取日期

--当天零点

select trunc(sysdate,'dd') from dual;

--当月一号

select trunc(sysdate,'mm') from dual;

--本年一月一号

select trunc(sysdate,'yyyy') from dual;

--可以根据需要自己去截取

 

关于毫秒:

Oracle 毫秒的存储必须字段类型为 timestamp(6) –数字表示存储的毫秒位数

--当前毫秒级时间

select to_char(current_timestamp,'yyyy-mm-dd hh24:mi:ss.ff6') from dual;

--字符串转为 timestamp类型

select to_timestamp('2012-02-03 10:29:46.453234','yyyy-mm-dd hh24:mi:ss.ff6') from dual;

--timestamp转为字符型

select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ss.ff6') from dual;

PS: ff后面的数字表示获得的毫秒位数,默认是6;一般ff3 获得三位毫秒数。 

如果你想把DATE类型转换成TIMESTAMP类型,就使用CAST函数。

select cast(sysdate as timestamp) from dual;

但是值得注意的是:在转换后的时间段尾部有了一段“.000000”。这是因为从date转换过来的时候,没有小数秒的信息,缺省为0。而且显示格式是按照参数NLS_TIMESTAMP_FORMAT定的缺省格式显示。当你把一个表中date类型字段的数据移到另一个表的timestamp类型字段中去的时候,可以直接写INSERT SELECT语句,oracle会自动为你做转换的。

注意: to_char函数支持date和timestamp,但是trunc却不支持TIMESTAMP数据类型。这已经清楚表明了在当两个时间的差别极度重要的情况下,使用TIMESTAMP数据类型要比DATE数据类型更确切。

还值得一提的是:毫秒的显示精度是6位,不过有效位是3位,即最大值达到999,满1000ms就进为1s。当然你想保存6位毫秒也是有办法的:

insert 值指定六位:to_timestamp('2012-02-03 10:29:46.453234','yyyy-mm-dd hh24:mi:ss.ff6')

Oracle date timestamp 毫秒 - 时间函数总结(转)的更多相关文章

  1. 【记录】Mybatis Generator生成数据对象Date/TimeStamp 查询时间格式化

    Mybatis Generator是很好的工具帮助我们生成表映射关联代码,最近博主遇到一个问题,找了很久才解决, 就是用Mybatis Generator生成实体类的时候,Date 时间无法格式化输出 ...

  2. php时间函数time(),date(),mktime()区别

    php时间函数time(),date(),mktime()区别   浏览:1161 发布日期:2014/12/18 分类:系统代码 关键字: php时间函数 time() date()mktime() ...

  3. php时间函数整理

    PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...

  4. php时间函数

    PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...

  5. php时间函数大锦集

    PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...

  6. [javascript]获取系统时间函数

    var oDate=new Date(); //初始化系统时间函数 alert(oDate.getHours()); //获取时 alert(oDate.getMinutes()); //获取分 al ...

  7. Oracle日期时间函数大全

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

  8. Oracle date 和 timestamp 区别

    1.DATE数据类型 这个数据类型我们实在是太熟悉了,当我们需要表示日期和时间的话都会想到date类型.它可以存储月,年,日,世纪,时,分和秒.它典型地用来表示什么时候事情已经发生或将要发生.    ...

  9. [转]ORACLE日期时间函数大全

    本文转自:http://www.cnblogs.com/chuncn/archive/2009/04/29/1381282.html ORACLE日期时间函数大全 TO_DATE格式(以时间: ::2 ...

随机推荐

  1. 洛谷p1732 活蹦乱跳的香穗子 二维DP

    今天不BB了,直接帖原题吧  地址>>https://www.luogu.org/problem/show?pid=1732<< 题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现 ...

  2. js判断数字、整数、字符串、布尔,特殊方法

    整数: function isInteger(obj) { return Math.floor(obj) === obj } isInteger(3) // true isInteger(3.3) / ...

  3. Intellij idea 添加浏览器

    最近的项目要做一个海康的网页端的监控,需要下载海康的插件,但是试验了一下,Chrome和IE的都不支持插件的显示,只有搜狗的显示,但是Idea的默认浏览器里面没有,所以要添加一个默认的浏览器 方法很简 ...

  4. .NET 使用中文编码

    在.Net Core中默认System.Text中不支持CodePagesEncodingProvider.Instance, System.Text.Encoding.CodePages.dll允许 ...

  5. eclipse打开失败

    以前eclipse运行好好的,某一次运行启动不了,一直图标那里转圈,不能启动, 运行eclipsec.exe后,查看发现出现以下错误 SLF4J: Class path contains multip ...

  6. MySQL中的排序(ORDER BY)

    当使用 SELECT FROM 时,如果不排 序,数据一般将以它在底层表中出现的顺序显示.这可以是数据最初添加到表中的顺序.但是,如果数据后来进行过更新或删除,则此顺 序将会受到MySQL重用回收存储 ...

  7. IOException parsing XML document from class path resource [WebRoot/WEB-INF/applicationContext.xml];

    parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io. ...

  8. web前端名人的博客微博Githu

    尤雨溪 vuejs作者 王垠  http://www.yinwang.org/ 20位活跃在Github上的国内技术大牛  1. lifesinger(玉伯)  Github主页:        ht ...

  9. linux下逻辑卷管理 调整分区大小

    [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 50 ...

  10. win下使用VM虚拟机安装Linux系统

    自己电脑上还是有个自己的虚拟机比较方便,之前用的Ubuntu,发现卡得不行. 现在装了个轻量级的Lubuntu,速度提升了不少. 1.下载Lubuntu,安装. 2.进入,设置root密码,初始化ro ...