注册 登录
壹佰网|ERP100 - 企业信息化知识门户 返回首页

的个人空间 https://www.erp100.com/?0 [收藏] [复制] [RSS]

日志

A Date or a Time?

已有 513 次阅读2008/3/10 19:46

It's important to be aware that every datetime value is composed of both a date and a time. Forgetting that point can lead to errors in your code. Following is a somewhat contrived example:

    DECLARE
x DATE;
BEGIN
x := SYSDATE;

IF x = TO_DATE('1-Apr-2005','dd-mon-yyyy')
THEN
DBMS_OUTPUT.PUT_LINE('Today is April Fool''s 2005');
END IF;
END;

The goal of this example is to display a message on 1 April, 2005, but the chance of that message ever being displayed is minimal. You'd need to run the code block exactly at midnight, to the second. That's because SYSDATE returns a time-of-day value along with the date.

To make the code block work as expected, you can truncate the value returned by SYSDATE to midnight of the day in question:

    x := TRUNC(SYSDATE);

Now, x still has a time of day, but that time of day is midnight. The TO_DATE function also returns a time of day, which, because no time of day was given, defaults to midnight (i.e., 00:00:00). Thus, no matter when on 1 April, 2005 you run this code block, the comparison will succeed, and the message will display.

评论 (0 个评论)

QQ|Archiver|小黑屋|手机版|壹佰网 ERP100 ( 京ICP备19053597号-2 )

Copyright © 2005-2012 北京海之大网络技术有限责任公司 服务器托管由互联互通
手机:13911575376
网站技术点击发送消息给对方83569622   广告&合作 点击发送消息给对方27675401   点击发送消息给对方634043306   咨询及人才点击发送消息给对方138011526

GMT+8, 2025/11/29 03:48 , Processed in 0.007343 second(s), 12 queries , File On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

返回顶部