I am getting data from table for certain date range, but the query not getting data for today's date. Why does this happen?
select *
from mytable
where action_date >= to_date('01/07/2015', 'DD/MM/YYYY')
and action_date <= to_date('22/07/2015', 'DD/MM/YYYY');
Result is not showing 22/07/2015 data.
Edit:
ACTION_DATE TIMESTAMP(6)
Sample date in that column :
22/07/15 12:47:18.000000000 PM
Try to add the time part:
select *
from mytable
where action_date >= to_date('01/07/2015 00:00:00,000000000', 'DD/MM/YYYY HH24:MI:SS,FF9')
and action_date <= to_date('22/07/2015 23:59:00,999999999', 'DD/MM/YYYY HH24:MI:SS,FF9');
If you only give the date part, the time part is automatically added with the actual time, so if the time part is later you do not get the row.
to_date('22/07/2015', 'DD/MM/YYYY')
this will equal to July, 22 2015 00:00:00
From Oracle document:
Oracle Database stores time in 24-hour format—HH:MI:SS. By default,
the time in a date field is 00:00:00 A.M. (midnight) if no time
portion is entered.
so action_date <=/>= will compare the data+time.
For the correct result add required time to the date field.
eg:
to_date('22/07/2015 12:56', 'DD/MM/YYYY HH24:MI')
it is because you are converting to_date in date, that results in 22/2/2015 00:00:00 and your action_date time for 22/7/2015 is with some time appended. you need to convert action_date in date.
select * from mytable where cast( action_date >= to_date('01/07/2015', 'DD/MM/YYYY') and cast( action_date as date) <= to_date('22/07/2015', 'DD/MM/YYYY');
I have changed the query by this way, its working as expected.
SELECT *
FROM mytable
WHERE action_date >= to_date('01/07/2015', 'DD/MM/YYYY')
AND TRUNC(to_date(TO_CHAR(action_date, 'DD/MM/YYYY'), 'DD/MM/YYYY')) <= TRUNC(to_date('22/07/2015', 'DD/MM/YYYY'));
You can use the below query.
select *
from mytable
where trunc(action_date) >= to_date('01/07/2015', 'DD/MM/YYYY')
and trunc(action_date) <= to_date('22/07/2015', 'DD/MM/YYYY');
This will perform a date-level comparison rather than one based on the date and exact time.
This is better than the (original) accepted answer, which misses a small period of time after 23:59.
select *
from mytable
where action_date >= to_date('01/07/2015', 'DD/MM/YYYY')
and action_date < to_date('22/07/2015', 'DD/MM/YYYY') + 1;
Or alternatively if you don't like the +1:
select *
from mytable
where action_date >= to_date('01/07/2015', 'DD/MM/YYYY')
and action_date < to_date('23/07/2015', 'DD/MM/YYYY');
The easiest solution is to use '< nextday' instead of boring '<= 23:59:59'.
select * from mytable
where action_date >= to_date('01/07/2015', 'DD/MM/YYYY')
and action_date < to_date('23/07/2015', 'DD/MM/YYYY');
Related
I have an oracle query for getting the values between 12pm and 12am ( each day) from dates 11/06/2021 to 16/06/2021. The query i have below will collect the info from the 11th up to the 15th , but will not obtain data for the 16th. Is there any other way i can solve this issue? Thanks a lot for your time
SELECT TO_CHAR(datadate, 'dd/mm/yyyy') ,
ROUND(AVG(BATTERYVOLTAGE),2) AVGVOLTAGE
FROM MYTABLE
WHERE BATTERYVOLTAGE!= 0
AND to_char(datadate, 'hh24:mi:ss') BETWEEN '12:00:00' and '24:00:00'
AND DATADATE BETWEEN TO_DATE('11/06/2021', 'dd/mm/yyyy')
AND TO_DATE('16/06/2021', 'dd/mm/yyyy')
GROUP BY TO_CHAR(datadate, 'dd/mm/yyyy')
ORDER BY 1
datadate BETWEEN to_date('11/06/2021', 'dd/mm/yyyy')
AND to_date('16/06/2021', 'dd/mm/yyyy')
includes only rows where datadate if before or at 2021-06-16 00:00:00. Everything past 2021-06-16 00:00:00 isn't included.
That's a common mistake. Don't use BETWEEN for such things. Use a range with >= and < instead.
datadate >= to_date('2021-06-11 12:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND datadate < to_date('2021-06-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
If anyone wondering how to do it.
Change the formatting of the last date.
DATADATE BETWEEN TO_DATE('12/06/2021', 'dd/mm/yyyy') AND (TO_DATE('22/06/2021', 'dd/mm/yyyy')+(1-1/24/60/60))
For more info
https://asktom.oracle.com/pls/apex/asktom.search?tag=date-queries-using-between
You will have problems try to query date and time separated; is easier if you join date and time and search for datetime value like to_date('2021-06-11 09:00:00','YYYY-MM-DD HH24:MI:SS') the range you'll get is since 9 at the limit you want
I would love some help. I am trying to search an oracle database for records between two dates.
And for each the result set find records that were entered between 00 and 5 but also 21 and 23:59:59 (for a night shift). Here is a MWE I tried...
select TO_CHAR(date_entered,'DD-MM-YYYY HH24:MI:SS') AS "Date Entered", other_rows FROM table_name
WHERE (TO_DATE('&From_Time', 'DD/MM/YYYY') <= date_entered) AND (TO_DATE('&To_Date', 'DD/MM/YYYY') >= date_entered)
--midnight to 5 am
AND EXTRACT(HOUR FROM CAST(date_entered AS TIMESTAMP)) BETWEEN 0 AND 5
--9pm to midnight
AND EXTRACT(HOUR FROM CAST(date_entered AS TIMESTAMP)) BETWEEN 21 AND 23
But this doesn't seem to be giving the expected results. Thank you for your extra eye.
You can use the following query:
SELECT
TO_CHAR(DATE_ENTERED, 'DD-MM-YYYY HH24:MI:SS') AS "Date Entered",
OTHER_ROWS
FROM
TABLE_NAME
WHERE
( TO_DATE('&From_Time', 'DD/MM/YYYY') <= DATE_ENTERED )
AND ( TO_DATE('&To_Date', 'DD/MM/YYYY') >= DATE_ENTERED )
--midnight to 5 am
AND ( TO_NUMBER(TO_CHAR(DATE_ENTERED,'HH24')) < 5
--9pm to midnight
OR TO_NUMBER(TO_CHAR(DATE_ENTERED,'HH24')) >= 21 )
You query looks fine, and using extract() is the correct way to isolate the (numeric) hour of a date.
But this:
--midnight to 5 am
AND EXTRACT(HOUR FROM CAST(date_entered AS TIMESTAMP)) BETWEEN 0 AND 5
Will actually give you dates whose time ranges from 00:00:00 to 05:59:59. You probably want this instead:
AND EXTRACT(HOUR FROM CAST(date_entered AS TIMESTAMP)) BETWEEN 0 AND 4
Which will give you times until 04:59:59.
Also, parentheses around the conditions in the where clause are superfluous here.
Consider:
SELECT
TO_CHAR(date_entered,'DD-MM-YYYY HH24:MI:SS') AS "Date Entered",
other_rows
FROM table_name
WHERE
TO_DATE('&From_Time', 'DD/MM/YYYY') <= date_entered
AND TO_DATE('&To_Date', 'DD/MM/YYYY') >= date_entered
AND EXTRACT(HOUR FROM CAST(date_entered AS TIMESTAMP)) BETWEEN 0 AND 4
AND EXTRACT(HOUR FROM CAST(date_entered AS TIMESTAMP)) BETWEEN 21 AND 23
As the title suggests, I'm simply trying to get the result set between midnight today and right now. So given the time of this post, I want data between 06/30/16 00:00:00 and 06/30/16 15:52:00. Why does the below query not return anything? Thanks.
SELECT * FROM tableName
WHERE event_date >= TO_DATE(TRUNC(SYSDATE) || ' 00:00:00', 'DD-MON-YY HH24:MI:SS')
AND event_date <= TO_DATE(TRUNC(SYSDATE))
SELECT *
FROM tableName
WHERE event_date BETWEEN TRUNC(SYSDATE) AND SYSDATE;
The solution was as follows:
SELECT * FROM tableName
WHERE event_date >= TO_DATE(TRUNC(SYSDATE))
I had forgotten that TRUNC(**SYSDATE)** by itself is perceived as midnight unless otherwise specified, so this does the trick.
select CASE
WHEN (shipment_date >=((to_char(shipment_date, 'dd-Mon-yyyy 08:00')::timestamp)) and shipment_date < ((to_char(shipment_date, 'dd-Mon-yyyy 20:00')::timestamp )))
THEN to_char(shipment_date, 'dd/MM/yyyy 08:00')
WHEN (shipment_date >=((to_char(shipment_date, 'dd-Mon-yyyy 20:00')::timestamp)) and shipment_date < ((to_char(shipment_date + interval '1' day, 'dd-Mon-yyyy 08:00')::timestamp )))
THEN to_char(shipment_date, 'dd/MM/yyyy 20:00')
END as shipment_date_sort,
COUNT(lotnumber)
from shipping.picklist_lots
inner join shipping.picklist on picklist_lots.picklist_id = picklist.picklist_id
where shipment_date >= '12/23/2014' and shipment_date < '12/24/2014'
GROUP BY shipment_date_sort
it doesnt give me the result which the shipment date between 24/12/2014 00:00 and 24/12/2014 08:00
i need it
where shipment_date >= '12/23/2014' and shipment_date < '12/24/2014'
Add a time value. According to your datetime value, 24/12/2014 00:00, it should be in format (dd/MM/yyyy 00:00)
where shipment_date >= '23/12/2014' and shipment_date <= '24/12/2014 23:59'
Based on logical query processing, you cannot use alias of your SELECT statement in group by because SELECT happens after GROUP BY so you need to duplicate your CASE statement into your GROUP BY part.
LOGICAL QUERY PROCESSING
Assuming this has a simple solution, but I can't find it.
I'm trying to do some logic on a DATE field in Oracle. My desire is to take a DATE field and subtract X hours from it.
For instance: SELECT A.MyDATE - 100 Hours from dual;
however, I need a result in a timestamp format 'YYYY-MM-DD hh:mm'.
I've tried CAST(A.MyDATE as TIMESTAMP) - NUMTODSINTERVAL(100/24,'day') however it didn't work.
I found out that the issue is that the MyDATE field when cast to a timestamp still contained some residual time elements. How can I reset these??
Thanks!
You can just do this with subtraction:
select a.MyDate - 100.0/24
To convert to varchar:
select to_char(a.MyDate - 100.0/24, 'YYYY-MM-DD')
And, if you want to get rid of that pesky time on the date:
select trunc(a.MyDate - 100.0/24) as JustTheDate
The formats and dates in my example can be changed to any other formats and dates:
SELECT To_Timestamp(To_Char(Sysdate - INTERVAL '100' HOUR, 'MM/DD/YYYY HH24:MI'), 'MM/DD/YYYY HH24:MI')
FROM dual
/
Output:
2/4/2013 10:18:00.000000000 AM
To remove time element add Trunc() to any of your dates...:
SELECT Trunc(To_Timestamp(To_Char(Sysdate - INTERVAL '100' HOUR, 'MM/DD/YYYY HH24:MI'), 'MM/DD/YYYY HH24:MI'))
FROM dual
/
Output: 2/4/2013
Conversion/Casting - when using other dates in place of sysdate then add formats as in my other examples:
SELECT CAST(SYSDATE AS TIMESTAMP) - INTERVAL '100' HOUR FROM dual
/
Output: 2/4/2013 10:26:35.000000000 AM
SELECT start_date tstamp_to_date, CAST(start_date AS timestamp) date_to_tstamp FROM
(
SELECT to_date(to_char(to_timestamp ('2013-02-07 10:07:47.000' , 'YYYY-MM-DD HH24:MI:SS.FF'),'DD-MON-YYYY HH24:MI:SS'), 'DD-MON-YYYY HH24:MI:SS') start_date
FROM dual
)
/
Output:
tstamp_to_date date_to_tstamp
-------------------------------------------------------
2/7/2013 10:07:47 AM 2/7/2013 10:07:47.000000 AM
In Oracle, a DATE always has a day and a time component. Depending on the tool you are using and your session's NLS_DATE_FORMAT, it is entirely possible that the tool may not display the time component when you look at the data. But that is simply a display question, it has no impact on the actual data.
If you want to subtract 100 hours from midnight on the day that MyDate represents
SELECT TRUNC(MyDate) - interval '100' hour
FROM dual
This will return a DATE. If you want to return a string in a particular format
SELECT TO_CHAR( TRUNC(MyDate) - interval '100' hour, 'YYYY-MM-DD hh:mi am' )
FROM dual
Note that I'm assuming that there was a typo in your question. I assume that you want to display the minutes after the hour (mi) rather than the month (mm).
I am trying to fetch the records which is older than 30 days (from Mod_date) and I am using the below query and it is returning all the data and I want only 30 days old data.
Sample :- Mod_date 03-NOV-12 12.00.00.000000000 AM
Query :-
select Mod_date from fil_cnfact where Mod_date <= sysdate -30 order by Mod_date asc ;