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.
|Archiver|小黑屋|手机版|壹佰网 ERP100
( 京ICP备19053597号-2 )
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.