Oracle between query ignoring end date time.
Below is my table.
Id Upload Date
1 05-JUL-17 12.02.11.309000000 PM
2 05-JUL-17 12.03.34.123000000 PM
3 05-JUL-17 12.04.15.334000000 PM
My requirement is to fetch the files between the given date time.
select * from fileupload where uploaddate between to_DATE('05-07-17 12:02:11', 'DD-MM-YY HH24:MI:SS') and to_DATE('05-07-17 12:04:15', 'DD-MM-YY HH24:MI:SS')
select * from fileupload where uploaddate between to_timestamp('05-07-17 12:02:11', 'DD-MM-YY HH24:MI:SS') and to_timestamp('05-07-17 12:04:15', 'DD-MM-YY HH24:MI:SS')
Both queries not returns the end date time it returns only two row.
Id Upload Date
1 05-JUL-17 12.02.11.309000000 PM
2 05-JUL-17 12.03.34.123000000 PM
But the expected result is
Id Upload Date
1 05-JUL-17 12.02.11.309000000 PM
2 05-JUL-17 12.03.34.123000000 PM
3 05-JUL-17 12.04.15.334000000 PM
why the end date time is not fetching when using between.
Any help will be greatly appreciated!!!!
The value to_DATE('05-07-17 12:04:15', 'DD-MM-YY HH24:MI:SS')
is equivalent to 05-JUL-17 12.04.15.000000000 PM
that comes before 05-JUL-17 12.04.15.334000000 PM
So the third result is not extracted because is not in the range for 334 ms.
To extract all the records you need to update the query not considering the milliseconds as follow:
select * from fileupload
where cast(uploaddate as date)
between to_DATE('05-07-17 12:02:11', 'DD-MM-YY HH24:MI:SS')
and to_DATE('05-07-17 12:04:15', 'DD-MM-YY HH24:MI:SS')
You need to use TO_TIMESTAMP()
.. It can handle milliseconds.
TO_DATE
doesn't handle milliseconds
to_DATE('05-07-17 12:04:15', 'DD-MM-YY HH24:MI:SS')
小于"05-JUL-17 12.04.15.334000000 PM"
(因为它是05-JUL-17 12.04.15.00
),尝试将to_DATE('05-07-17 12:04:15', 'DD-MM-YY HH24:MI:SS')
更改为to_DATE('05-07-17 12:04:16', 'DD-MM-YY HH24:MI:SS')
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.