We have a table which has a DATE column d.
I'd like to get all the rows where the d column is greater / lower than some value, regardless of the date.
For example
| d |
-------------------
|2009/11/1 15:55:23|
--------------------
|2009/11/2 15:55:23|
--------------------
|2009/11/3 15:55:23|
--------------------
|2009/11/3 17:55:23|
--------------------
For example, If I want all the records marked after 5 PM:
select d
from my_table
where extract( hour from d ) > TO_DATE ('17:00:00','HH24:MI:SS')
This should return only one record
|2009/11/3 17:55:23|
I don't know if this is the best way to do it, but I get an error on the extract function:
ORA-30076: invalid extract field for extract source
Cause: The extract source does not contain the specified extract field.
Is there a better way to do this? Whats up with that error? extract only available for sysdate, as in all examples i've found?
Thanks in advance
When using a literal you must specify your date in the format YYYY-MM-DD and you cannot include a time element. Remember that the Oracle date datatype includes a time element, so the date without a time portion is equivalent to 1995-12-31 00:00:00 .
Discussion: To calculate the difference between the timestamps in Oracle, simply subtract the start timestamp from the end timestamp (here: arrival - departure ). The resulting column will be in INTERVAL DAY TO SECOND . The first number you see is the number of whole days that passed from departure to arrival .
Answers. You can use the TO_DATE function. Convert both to timestamp and compare.
This can be easily done using equals to(=), less than(<), and greater than(>) operators. In SQL, the date value has DATE datatype which accepts date in 'yyyy-mm-dd' format. To compare two dates, we will declare two dates and compare them using the IF-ELSE statement.
Don't have an Oracle database to test with at the moment but I think the following should work:
SELECT *
FROM MY_TABLE t
WHERE EXTRACT(HOUR FROM CAST(t.D AS TIMESTAMP)) > 16;
But I don't understand why the CAST would be needed as this page says that HOUR is a valid field in a DATE.
Share and enjoy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With