Oracle date timestamp 毫秒 - 时间函数总结(转)
原文地址: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 毫秒 - 时间函数总结(转)的更多相关文章
- 【记录】Mybatis Generator生成数据对象Date/TimeStamp 查询时间格式化
Mybatis Generator是很好的工具帮助我们生成表映射关联代码,最近博主遇到一个问题,找了很久才解决, 就是用Mybatis Generator生成实体类的时候,Date 时间无法格式化输出 ...
- php时间函数time(),date(),mktime()区别
php时间函数time(),date(),mktime()区别 浏览:1161 发布日期:2014/12/18 分类:系统代码 关键字: php时间函数 time() date()mktime() ...
- php时间函数整理
PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...
- php时间函数
PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...
- php时间函数大锦集
PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...
- [javascript]获取系统时间函数
var oDate=new Date(); //初始化系统时间函数 alert(oDate.getHours()); //获取时 alert(oDate.getMinutes()); //获取分 al ...
- Oracle日期时间函数大全
ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...
- Oracle date 和 timestamp 区别
1.DATE数据类型 这个数据类型我们实在是太熟悉了,当我们需要表示日期和时间的话都会想到date类型.它可以存储月,年,日,世纪,时,分和秒.它典型地用来表示什么时候事情已经发生或将要发生. ...
- [转]ORACLE日期时间函数大全
本文转自:http://www.cnblogs.com/chuncn/archive/2009/04/29/1381282.html ORACLE日期时间函数大全 TO_DATE格式(以时间: ::2 ...
随机推荐
- Java 静态方法不能重写但可以被子类静态方法覆盖
强调 静态方法是属于类的,只存在一份,会被该类的所有对象共享.不可以被重写. 静态方法可以被子类继承,但是不可以被子类重写 class door{ } class wood_Door extends ...
- python学习打卡 day07 set集合,深浅拷贝以及部分知识点补充
本节的主要内容: 基础数据类型补充 set集合 深浅拷贝 主要内容: 一.基础数据类型补充 字符串: li = ["李嘉诚", "麻花藤", "⻩海峰 ...
- WAI-ARIA无障碍网页应用属性完全展示
本文为原创辛苦之作,尊重劳动,转载请注明来自张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com]本文地址:http://www.zhangxinxu.com/wordpress ...
- IDEA @Autowired 出现红色下划线 报红
例如: 解决方法:
- art-template实战
内容div <div id="sku-cont"> <div class="form-group col-lg-12"> <div ...
- SpringMVC 处理Date类型数据@InitBinder @DateTimeFormat 注解 的使用
使用SpringMVC的时候,需要将表单中的日期字符串转换成对应JavaBean的Date类型,而SpringMVC默认不支持这个格式的转换,解决方法有两种,如下: 方法一 . 在需要日期转换的Con ...
- 微信小程序不可操作dom节点
//根据历史记录快速搜索 fastSearch:function(e){ console.log(e.currentTarget.id) wx.navigateTo({ url: '../search ...
- vue element-ui 日期选择器组件 日期时间格式化
vue element-ui 组件开发大大提高了我们的效率,但有时候并不能满足我们的需求,例如时间,日期组件: element-ui 日期返回的格式是这样的,看下图: 但我们要的是另一个格式 , 如下 ...
- eslint简单的规范
module.exports = { root: true, parser: 'babel-eslint', parserOptions: { sourceType: 'module' }, // h ...
- json包
1.官网下载 2.pom文件下载: <dependency> <groupId>net.sf.json-lib</groupId> <artifactId&g ...