centos 下载oracle_Oracle 之DATE类型的“小陷阱”

转载老刘的文章,问题虽小,但自己最近就踩了一下这个坑。

朋友提了个问题,虽然不是难题,但确实很细微,很可能就掉进了陷阱中,假设执行日期是2020-08-26,创建测试数据,

SQL> create table t01 (id number, insert_time date); Table created.SQL> insert into t01 values(1, to_date('2020-08-25','yyyy-mm-dd'));1 row created.

需求是检索insert_time是昨天的记录,他用这条SQL,通过trunc(sysdate)-1得到前一天的日期作为条件,乍看很合理,但是结果是0,

SQL> select * from t01 where insert_time > trunc(sysdate)-1;no rows selected

但是他使用trunc(sysdate)-2,能得到记录,“-2”不是前天?不是昨天啊?

SQL> select * from t01 where insert_time > trunc(sysdate)-2;        ID INSERT_TIME---------- ------------------         1 25-AUG-20

其实这个问题很简单,DATE类型在Oracle中定义包含了“年、月、日、时、分、秒”,即使像上例中,to_date('2020-08-25','yyyy-mm-dd'),只是指定年月日,还是包括时分秒,默认值是0,

DATE Data Type
The DATE data type stores date and time information. Although date and time information can be represented in both character and number data types, the DATE data type has special associated properties. For each DATE value, Oracle stores the following information: year, month, day, hour, minute, and second.


The default date values are determined as follows:
The year is the current year, as returned by SYSDATE.
The month is the current month, as returned by SYSDATE.
The day is 01 (the first day of the month).
The hour, minute, and second are all 0.

trunc(sysdate)-1是指"昨天00:00:00",如下SQL,能证明,

SQL> select to_char(trunc(sysdate),'yyyy-mm-dd hh24:mi:ss') from dual;TO_CHAR(TRUNC(SYSDA-------------------2020-08-26 00:00:00

条件中的>trunc(sysdate)-1,就是超过昨天00:00:00的,而存储的数据,to_date('2020-08-25','yyyy-mm-dd'),就是昨天00:00:00,因此无结果,这是正常的,需要用insert_time > trunc(sysdate)-2,才可以找到。

但如果插入时就指定了时分秒,就会不同了,

SQL> insert into t01 values(1, to_date('2020-08-25 01:00:00','yyyy-mm-dd hh24:mi:ss'));1 row created.

是可以找到的,

SQL> select * from t01 where insert_time > trunc(sysdate)-1;        ID INSERT_TIME---------- ------------------         1 25-AUG-20

格式化下,会更清楚些,的确是找到了01:00:00的,

SQL> select id, to_char(insert_time,  'yyyy-mm-dd hh24:mi:ss') from t01 where insert_time > trunc(sysdate)-1;        ID TO_CHAR(INSERT_TIME---------- -------------------         1 2020-08-25 01:00:00

这个问题,虽然细微,但稍不注意,可能在写程序时,就会忽视,造成边界值隐患,因此,还是要关注下。

转文至此。

欢迎关注个人微信公众号“一森咖记”da0a72446628332c9c8bf4efcf836fe0.png

a72e06136596f06417b3524ddedc682d.png

   

近期热文

你可能也会对以下话题感兴趣。点击链接便可查看。

  • Oracle ADG同步技术,DBA必备的一种“后悔药”

  • Oracle 11g及12c+版本下为啥有些表不能exp导出?

  • OpenJDK和Oracle JDK有什么区别和联系?

  • 神技_如何快捷下载Oracle补丁的方法?!

  • 趋势篇:oracle 11g,12c,18c,19c之support lifetime

  • Configuring Kernel Parameters about SHMMAX on HP

  • 对recursive calls的深刻理解

  • Centos能不能替换RHEL?

  • RAC1 服务器要重启,喂:RAC2你先顶一下,咋搞?!

  • 对recursive calls的深刻理解

  • OpenJDK和Oracle JDK有什么区别和联系?

  • Centos能不能替换RHEL?

  • PLSQL Developer中控制结果集表格自动提交

  • alter table T1 add column新增字段执行时间超长,咋回事?

  • Oracle 11g 异机rman恢复报错ORA-27302:failure occurred at: sskgpcreates

  • 年末总结_聊一聊数据库行业的“继往开来”

  • Materialized view物化视图的一个简单应用场景

  • 干货:RHEL7.2生产环境下双节点12c RAC搭建实操

  • 【干货篇】在国内外数据库百家争鸣的时代,DBA们该何去何从?

  • LINUX环境:MySQL和Oracle开机自启动,咋搞?Logminer:oracle人为误操作之恢复神器

  • What:ASM自动脱落了

  • 实操:12C RAC环境下的ADG同步库搭建

  • “神器”:Oracle日志采集分析工具——TFA

  • Oracle Rac:关闭透明大页的原因及方法

  • 实操篇:Oracle 19c的安装部署

  • 浅谈MySQL三种锁:全局锁、表锁和行锁

  • Oracle如何访问MySql:透明网关

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值