choose from a time period oracle sql - sql

I have timestamp date format in column FIRST_DATE and i need to choose time period from a certain hour, for ex. all from 18:00 10.05.21 to 18:00 11.05.2021
the problem is that date column in timestamp format - FIRST_DATE:
10/05/2020 0:00:03,000000 TIMESTAMP(6)
so i tried to use it:
select
count(*)
from TABLE
where to_char(FIRST_DATE, 'HH24:MI')>='18:00'
so with this way i was able to limit the start period by time, but if i add date to this my conditions stop working
and to_char(FIRST_DATE, 'DD-MON-YY')>='10-MAY-21'
how can i correct my script to select all from 18:00 10.05.21 to 18:00 11.05.2021

Don't compare dates (or timestamps) with strings. '18:00' and '10-MAY-21' are strings. Use TO_TIMESTAMP with appropriate format mask, e.g. (lines #5 and 6):
SQL> with test (first_date) as
2 (select to_timestamp('10/05/2020 23:00:03,000000', 'dd/mm/yyyy hh24:mi:ss,ff3') from dual)
3 select *
4 from test
5 where first_date between to_timestamp('10/05/2020 18:00:00,000000', 'dd/mm/yyyy hh24:mi:ss,ff3')
6 and to_timestamp('11/05/2020 18:00:00,000000', 'dd/mm/yyyy hh24:mi:ss,ff3')
7 /
FIRST_DATE
---------------------------------------------------------------------------
10.05.20 23:00:03,000000000
SQL>

Related

Hours and minutes between 2 incorrectly formatted datetimes

So i have some timestamps in a DB and i want to get the hours and minutes difference from them
The problem is the timestamp portion is formatted incorrectly where the hour is always 12 and the minutes portion is actually the hours and the seconds is actually the minutes.
Example DB timestamp: 10/1/2020 12:08:52 AM
So in the above example the time is actually 8:52 AM not 12:08 AM
How can i convert this datetime to something i can use in order to calculate the difference in minutes and hours between these 2 oddly formatted timestamps?
My ideal end goal is something that displays the difference in the HH:MM format
EDIT: the timestamps in oracle actually look like below, and in this eaxmple the 12 means nothing and 18 is actually the hours.
Example of what I'm looking for:
01-OCT-20 12.18.44.000000000 AM - 01-OCT-20 12.12.42.000000000 AM
Output: 06:02 . so the timespan would be 6 hours and 2 minutes in this case.
Thanks,
You can turn your string to an Oracle date (resp timestamp) with to_date() (resp to_timestamp()):
to_timestamp(mystring, 'dd/mm/yyyy ss:hh12:mi am')
Then you can use date arithmetics to compute the difference. Substrating timestamps gives you an interval, which is pretty much what you seem to be looking for, so:
to_timestamp(mystring1, 'dd/mm/yyyy ss:hh12:mi am')
- to_timestamp(mystring2, 'dd/mm/yyyy ss:hh12:mi am')
as myinterval
Like so?
(my default date format is 'yyyy-mm-dd hh24:mi:ss' in Oracle ...)
WITH
indata(sdb) AS (
SELECT '10/1/2020 12:08:52 AM' FROM dual
UNION ALL SELECT '10/1/2020 12:08:52 PM' FROM dual
)
SELECT
TO_TIMESTAMP(sdb,'dd/mm/yyyy 12:hh:mi AM') AS ts
FROM indata;
-- out ts
-- out ---------------------
-- out 2020-01-10 08:52:00
-- out 2020-01-10 20:52:00

ORACLE using TO_DATE to check if item is within last hour

I have a query that I am trying to use TO_DATE to check if ERROR_DT is a data that is within one hour of the current time
Here is what I have so far
SELECT BERROR_DT FROM SomeTable
WHERE ERROR_DT>=TO_CHAR(TO_DATE( SYSDATE, 'MM/DD/YYYY HH12:MI:SS AM') -1, 'fmMM/DDfm/YYYY HH12:MI:SS AM');
Error_DT has a value of (e.g.) 5/18/2020 6:45:15 PM
When I run this I get
ORA-01830: date format picture ends before converting entire input string
I followed the said link and it still is not working. How would I fix this so that I can still remove all 0s in front of the month and the date?
I would suggest converting the date string to the corresponding date value, then do the comparison:
select berror_dt
from sometable
where to_date(error_dt, 'fmMM/DD/YYYY HH12:MI:SS AM') >= sysdate - interval '1' hour
Bottom line, you should fix your data model and store dates as a date-like datatype rather than as a string. The above predicate is not efficient, because the conversion needs to be executed for each and every value of error_dt before the filtering applies, hence defeating an existing index on the column.
Obviously wrong thing you're doing is applying TO_DATE to SYSDATE which is a function that returns DATE datatype.
What you could do is to subtract sysdate and error_dt (I presume its datatype is DATE as well) and see whether difference is less than 1 hour. As difference of two dates is number of days, you have to divide it by 24 (as there are 24 hours in a day).
Something like this:
SQL> alter session set nls_date_format = ' dd.mm.yyyy hh:mi:ss am';
Session altered.
SQL> with test (id, error_dt) as
2 (select 1, to_date('18.05.2020 10:30:15 PM', 'dd.mm.yyyy hh:mi:ss am') from dual
3 union all
4 select 2, to_date('18.05.2020 05:20:55 AM', 'dd.mm.yyyy hh:mi:ss am') from dual)
5 select t.id, t.error_dt, sysdate
6 from test t
7 where sysdate - t.error_dt < 1 / 24;
ID ERROR_DT SYSDATE
---------- ----------------------- -----------------------
1 18.05.2020 10:30:15 PM 18.05.2020 11:02:24 PM
SQL>
If ERROR_DT is a DATE value you just need to use something like
SELECT BERROR_DT
FROM SomeTable
WHERE ERROR_DT >= SYSDATE - INTERVAL '1' HOUR
or if you prefer to use old-fashioned pre-INTERVAL calculations
SELECT BERROR_DT
FROM SomeTable
WHERE ERROR_DT >= SYSDATE - (1/24)

date range in oracle apps report

The following sql when run with these parameters,
:P_COMP_DATE_FROM = '15-NOV-2015'
:P_COMP_DATE_TO = '15-NOV-2015'
compares as between '15-NOV-2015 00:00:00' and '15-NOV-2015 00:00:00'
Select Ordered_date
From xxcost_rep
Where DATE_COMPLETED BETWEEN NVL(fnd_date.canonical_to_date(:P_COMP_DATE_FROM), DATE_COMPLETED) AND NVL(fnd_date.canonical_to_date(:P_COMP_DATE_TO)), DATE_COMPLETED);
how can I compare this as start of the day and end of the day, so can display the correct result in the range.
I am trying the following to add 86399 seconds to make it the end of the day, but receiving error:
WHERE DATE_COMPLETED BETWEEN NVL(fnd_date.canonical_to_date(:P_COMP_DATE_FROM), DATE_COMPLETED) AND NVL(fnd_date.canonical_to_date(to_date(:P_COMP_DATE_TO,'DD-MON-YYYY')+interval '86399' second), DATE_COMPLETED)
{P_TO_CUSTOMER=, P_COMP_DATE_FROM=2015/11/15 00:00:00, P_COMP_DATE_TO=2015/11/15 00:00:00, P_TO_ORDER_NUMBER=, P_CUST_REGION=, P_TO_DATE=, P_JOB_STATUS=, P_FROM_DATE=, P_FROM_ORDER_NUMBER=, P_FROM_CUSTOMER=}
Calling XDO Data Engine...
--SQLException
java.sql.SQLDataException: ORA-01861: literal does not match format string
ORA-01861: literal does not match format string
The above error is because the date literal doesn't match with the format mask.
For example,
SQL> SELECT TO_DATE('2015118','yyyy/mm/dd') FROM dual;
SELECT TO_DATE('2015118','yyyy/mm/dd') FROM dual
*
ERROR at line 1:
ORA-01861: literal does not match format string
You might be storing dates as string, and there might be strings with different date formats. Therefore, your function fnd_date.canonical_to_date might be failing for such date literals while converting into DATE using TO_DATE.
Also, you should not depend on your client's NLS date format. Remember, TO_DATE is NLS dependent. You should explicitly mention the format mask.
For example,
SQL> SELECT to_date('11/18/2015 00:00:00', 'mm/dd/yyyy hh24:mi:ss') date_from,
2 to_date('11/18/2015 23:59:59', 'mm/dd/yyyy hh24:mi:ss') date_to
3 FROM dual;
DATE_FROM DATE_TO
------------------- -------------------
11/18/2015 00:00:00 11/18/2015 23:59:59
In your case, you need to compare the dates. You could do it like the below example,
SQL> WITH DATA AS(
2 SELECT DATE '2015-11-18' dt FROM dual
3 )
4 SELECT * FROM DATA
5 WHERE dt
6 BETWEEN to_date(
7 to_char(dt, 'mm/dd/yyyy')||' 00:00:00',
8 'mm/dd/yyyy hh24:mi:ss'
9 )
10 AND to_date(
11 to_char(dt, 'mm/dd/yyyy')||' 23:59:59',
12 'mm/dd/yyyy hh24:mi:ss'
13 );
DT
-------------------
11/18/2015 00:00:00
UPDATE
For the first part where you just need the start time, you don't have to add the time portion as 00:00:00since DATE has both date and time elements. When you do not mention the time portion, it defaults to midnight i.e. 00:00:00.
For example, add INTERVAL '86399' SECOND:
SQL> SELECT DATE '2015-11-18' from_date,
2 DATE '2015-11-18' + INTERVAL '86399' SECOND to_date
3 FROM dual;
FROM_DATE TO_DATE
------------------- -------------------
11/18/2015 00:00:00 11/18/2015 23:59:59

Oracle DB select between dates

I would like to query number of records between night 12.01 AM to 11.59 PM but issue is, I would like to schedule this query so I cant specify any hard coded dates.
Query should pull number of records for query date between 12.01 AM to 11.59 PM.
Could someone please help me on this.
Query should pull number of records for query date between 12.01 AM to 11.59 PM.
You could do it as:
TRUNC gives you date element truncating the time portion
convert the SYSDATE into string using TO_CHAR
then concatenate the time element
finally convert everything back to DATE
SYSDATE returns the current date and time set for the operating system on which the database resides. The datatype of the returned value is DATE, and the format returned depends on the value of the NLS_DATE_FORMAT initialization parameter.
So, you don't have to hard-code any DATE value if you want to execute the query everyday.
Use the following in the filter predicate:
BETWEEN
TO_DATE(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY') ||' 00:01', 'MM/DD/YYYY HH24:MI')
AND
TO_DATE(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY') ||' 23:59', 'MM/DD/YYYY HH24:MI')
Demo
SQL> alter session set nls_date_format = 'MM/DD/YYYY HH24:MI:SS';
Session altered.
SQL> SELECT to_date(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY')
2 ||' 00:01', 'MM/DD/YYYY HH24:MI') start_dt ,
3 to_date(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY')
4 ||' 23:59', 'MM/DD/YYYY HH24:MI') end_date
5 FROM dual;
START_DT END_DATE
------------------- -------------------
05/06/2015 00:01:00 05/06/2015 23:59:00
SQL>
So, you don't have to put any hard-coded value for current date, the SYSDATE will take care of it. All you are doing is:
TRUNC gives the date element by truncating the time portion.
Then concatenating the required time element
Converting the entire string into DATE using TO_DATE
I would like to schedule this query so I cant specify any hardcord dates
To schedule the query to execute everyday, you could use DBMS_SCHEDULER.
I'm going to assume you want everything that happens during the date of interest. So you want everything from and including midnight of that day and before midnight of the next day.
declare
AsOf Date = date '2015-01-01 13:14:15';
select ...
from tablename
where tabledate >= trunc( AsOf )
and tabledate < trunc( AsOf ) + 1;
If you know the date doesn't have a time portion, just can eliminate the calls to trunc. But you may want to keep them just in case.

How can query last 5 minutes of records?

How I can to see last 5 mins record before the current time through sql query how i can do this.
The format of time stamp is
03/25/2014 14:00:00
I used this query for the same
SELECT Time stamp FROM TABLE
WHERE S >1 AND SUBSTRING((Time stamp,15,2)-5)
is this fine of any other way to do the same
If you are using MySQL and your timestamp column is of data type datetime you can do
SELECT Timestamp
FROM your_table
WHERE Timestamp >= now() - interval 5 minute
If your timestamp is a date column, you can simply do:
select t.*
from table t
where t.timestamp >= sysdate - 5/(24*60)
Things are a bit more interesting if timestamp is a character column. Then you need to translate it to a date/time:
select t.*
from table t
where to_date(t.timestamp, 'MM/DD/YYYY HH24:MI:SS') >= sysdate - 5/(24*60)
select *
from the_table
where timestamp_column <= timestamp '2014-03-25 14:00:00' - interval '5' minute;
This assumes that timestamp_column is defined with the data type timestamp.
If it isn't you should stop now and re-define your table to use the correct data type.
The expression timestamp '2014-03-25 14:00:00' is a (ANSI SQL) timestamp literal.
It's equivalent to to_timestamp('2014-03-25 14:00:00', 'yyyy-mm-dd hh24:mi:ss') but I prefer the ANSI literal because it's less typing and works across multiple DBMS.
Here's an example of how to get 5 minutes ago in oracle. subtracting from a timestamp in increments of 1 where 1 is a day. so 5 minutes would be 5/(24hours*60min) of a day.
SELECT sysdate, sysdate-(5/(24*60)) as min_Ago5 from dual