简体   繁体   中英

When comparing DATE with SYSDATE in oracle SQL, do they consider time?

I'm running the below code on oracle sql Developer, I have two questions. (Apologies in advance, I'm only learning sql)

1.Can you input a date, into Test_Date of type DATE, in this format excluding time? -> '30-Apr-15' 2.When comparing DATE with SYSDATE, and DATE is in the format '30-Apr-15', does it take the current time into consideration? Does it consider the current time at all?

The problem is If the test were to be done on the same day as the data entry, then would there be an error?

Many Thanks!

BEGIN
IF( :NEW.Test_Date < DATE '1900-01-01' OR 
:NEW.Test_Date > SYSDATE )
THEN
RAISE_APPLICATION_ERROR ( -20001, 
'Test Date must be later than Jan 1, 1900 and earlier than time of data 
entry' );
END IF;
END;

DATE, in this format excluding time? -> '30-Apr-15'
...
DATE is in the format '30-Apr-15'

A date is stored in an Oracle-specific internal representation. You can't actually have a date without a time. But if you do to_date('30-APR-15', 'DD-MON-RR') then since you are not supplying the time components they default to zero - so the date that creates is actually midnight on that day. You can convert it back to a string with to_char() to verify that.

It is better to use 4-digit years, and to avoid month names/abbreviations, and implicit conversions, or to rely on NLS serrings. Explicitly convert using an unambiguous format model if possible. (Of course, the string you supply has to be in the same format.)

You can also use a date literal, as you already are later. But similarly, DATE 1900-01-01 still evaluates to a date with a time - it is midnight on Jan. 1, 1900. (There is also a timestamp literal that allows you to supply the time as well.)

When comparing DATE with SYSDATE ... does it take the current time into consideration? Does it consider the current time at all?

Yes, it uses the time from both the date value - which may be midnight, but is always there - and sysdate .

if the test were to be done on the same day as the data entry, would there be an error?

If the data entry used a date literal or a date string/format with no time, then the column value will be at midnight on the specified day - so it depends what date you used. If the insert used today's date, date '2017-12-10' , then it would be midnight this morning which is earlier than sysdate , so you would not raise an exception. Even if the insert actually used sysdate itself that would be OK as it isn't later than itself.

The insert would have to either specify a future date, or an explicit time later today, to get the exception. Or a date before 1900, of course.

——-

If you want to 'ignore' the time, you can use the trunc() function. For example:

IF TRUNC(:NEW.Test_Date, 'DD') > TRUNC(SYSDATE, 'DD') THEN

which compares midnight on Test_Date wih midnoght this morning. But from the exception message you are raising, you do want to comoares the time anyway.

In Oracle the data type "date" DOES contain both date and time accurate to one second. Technically therefore the answer to your first question is no, BUT, when you input a date like this to_date('30-Apr-15','DD-Mon-YY') the time components are set to 00:00:00 so the full value of that date would be 2015-04-30 00:00:00

When comparing an input of to_date('30-Apr-15','DD-Mon-YY') to sysdate is time (of day) considered? Yes. sysdate returns both current date and time to the nearest second, and as described above the input value is set to a time of 00:00:00 if not specified by the input.


Please note that there is no "format" in the way dates are stored (there are stored as sets of numbers) so they are not stored in a human readable format at all. If you describe a date in a format of .... it gets confusing because it might mean your "date" is stored as a string. So, if you really are discussing the Oracle "date data type" do not describe it as being "in a format".

On the other hand, if you ever do have a question involving date information stored as strings, please state that clearly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM