Query to get the result between two date time stamp - oracle

I want to fetch some records which should give results based on certain time.
That means from 2.30 Am to 6.00 AM.
I tried using the between function, but i am not getting.
MTRDCRE between (to_char(to_date('16-03-2016 02:50:00', 'dd-mm-yyyy hh24:mi:ss')) , to_char(to_date('16-03-2016 05:50:00', 'dd-mm-yyyy hh24:mi:ss')))

Try this:
select *
from yourtable
where MTRDCRE >= to_char(to_date('16-03-2016 02:50:00', 'dd-mm-yyyy hh24:mi:ss'))
and MTRDCRE <= to_char(to_date('16-03-2016 05:50:00', 'dd-mm-yyyy hh24:mi:ss'))
If your column MTRDCRE is date you don't need to_char().

Related

Oracle query not giving result for current_date

What is the query in Oracle to fetch the data for current_date
the column end_date is like the following
end_date
27-10-16 03:35:00.000000000 PM
23-11-16 11:15:00.000000000 AM
02-11-16 03:00:00.000000000 PM
08-11-16 09:00:00.000000000 AM
Like I am running the following query as
Select * from table1
where end_date < TO_DATE('2017-04-11 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
it is running successfully, but when i replace the query with the current date ... it is not giving the results
Select * from table1
where end_date < TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS')
could someone tell me what is the cause the second query is not giving results.
CURRENT_DATE returns date. There is no need to use TO_DATE. The below query should be enough.
Select * from table1
where end_date < current_date;
If you run the below query you'll understand what went wrong for you. Year becomes 0011.
SELECT TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
Please note that CURRENT_DATE returns the current date in the session time zone. SYSDATE returns the current date and time set for the operating system on which the database resides. This means that CURRENT_DATE and SYSDATE can return different results. You can have a look at this
The query worked like this :
Select * from table1
where trunc(end_date) < trunc(sysdate)
Trunc is used to compare the both dates and it fetch the results.
CURRENT_DATE is already a DATE value. You can format the output using to_char if you want.
end_date < CURRENT_DATE should do the job. Or you can set the nls parameter accordingly for a better readability.
If you are comparing only date, without timestamp, you can go with trunc()

Get Data from a date range in oracle

I am trying to get data for a specific date range, I do it like this:
select EntryID
min(dtUsedDate) dtFirstUsedDate,
max(dtUsedDate) dtLastUsedDate
from tblEntrance e
where e.dtUsedDate between to_date('2016-02-08 10:00:00', 'yyyy-mm-dd hh24:mi:ss')
AND to_date('2016-02-08 10:15:59', 'yyyy-mm-dd hh24:mi:ss')
(dtFirstUsedDate and dtLastUsedDate are getting called in a outer select, so don't worry much about them for now)
What I get are the entrances (records) that are only between those dates/time, so dtFirstUsedDate and dtLastUsedDate, both toghether in between that date range. But what I need is those two to be independent, like the dtFirstUsed must be between that max and min date and dtLastUsed must be between that max and min date.
I hope my question is understandable and someone can help me.
I think you are looking for this..
SELECT e.EntryID
,MIN(e.dtUsedDate) dtFirstUsedDate
,MAX(e.dtUsedDate) dtLastUsedDate
FROM tblEntrance e
GROUP
BY e.EntryID
HAVING MIN(e.dtUsedDate) BETWEEN TO_DATE('2016-02-08 10:00:00', 'yyyy-mm-dd hh24:mi:ss')
AND TO_DATE('2016-02-08 10:15:59', 'yyyy-mm-dd hh24:mi:ss')
AND MAX(e.dtUsedDate) BETWEEN TO_DATE('2016-02-08 10:00:00', 'yyyy-mm-dd hh24:mi:ss')
AND TO_DATE('2016-02-08 10:15:59', 'yyyy-mm-dd hh24:mi:ss')

oracle to_date with format doesn't show time

I have simple calculation, I subtract interval from date with time:
select TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00') from dual;
It works fine, the result: 2016-12-05 22:59:59
but it doesn't work correctly with timezones, so the next approach solves the problem with timezone. I just wrap expression with to_date() one more time
select TO_DATE(
TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00')) from dual;
but now it turns time to zeros. Result should be: 2016-12-05 22:59:59 but actual: 2016-12-05 00:00:00
If I add format to the outer to_date as this:
select to_date( TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00'), 'YYYY-MM-DD HH24:MI:SS') from dual;
The result become very strange: 0005-12-16 00:00:00
What I'm doing wrong?
DATE data type does not support any time zone functions, you must use TIMESTAMP WITH TIME ZONE for that.
Your query
SELECT TO_DATE( TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - TO_DSINTERVAL('00 0:05:00'), 'YYYY-MM-DD HH24:MI:SS')
FROM dual;
does following:
Create a DATE '2016-12-05 23:04:59'
Subtract interval '00 0:05:00'
Cast to a VARCHAR2 (using NLS_DATE_FORMAT format)
Cast to a DATE using YYYY-MM-DD HH24:MI:SS format
In case your NLS_DATE_FORMAT would be equal to YYYY-MM-DD HH24:MI:SS this query returns correct output.
Use this one:
SELECT TO_TIMESTAMP('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - TO_DSINTERVAL('00 0:05:00')
FROM dual;
TO_DATE(... works as well. If you need time zone support you must do:
SELECT TO_TIMESTAMP_TZ('2016-12-05 23:04:59 Europe/Berlin', 'YYYY-MM-DD HH24:MI:SS TZR') - TO_DSINTERVAL('00 0:05:00')
FROM dual;
TO_DATE( char, fmt, nls ) takes VARCHAR2 arguments.
Performing TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00') returns a DATE datatype which when you pass it to TO_DATE() oracle will cast it to a VARCHAR2 datatype so it matches the expected datatype of the argument (implicitly calling TO_CHAR( value, NLS_DATE_FORMAT ) to perform this cast) and then convert this back to a DATE datatype.
You just need to do:
SELECT TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS')
- to_dsinterval('00 0:05:00')
FROM DUAL;
If you want to handle time zones then use a TIMESTAMP AT TIME ZONE and just convert it to whatever timezone you want to store the date at:
SELECT TIMESTAMP '2016-12-05 23:04:59 Europe/Paris' AT TIME ZONE 'UTC'
FROM DUAL;
(Will create your timestamp in Paris' time zone and convert it to the correct time in the UTC time zone).

Alter a datetime in Oracle and set time to 9 am

i have 28-APR-2016 10:05:07 date as parameter in stored procedure. This may be the current time also as string date.
i need to set the time to 9 am to check the shift start timing.
SELECT TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS') FROM dual;
I am new to oracle. Help is appreciated.
If you want the date with 9:00 a.m., then you can do:
SELECT TRUNC(TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS')) + 9/24.0
FROM dual;
You can also use:
SELECT TRUNC(TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS')) + INTERVAL '9' HOUR
FROM dual;
I'm just old-fashioned so I tend to use the first method.

Wrong difference in minutes in Oracle (DATE2-DATE1)*24*60

I'm trying to calculate difference in minutes between two dates in Oracle with this testing query:
SELECT
(DATE2-DATE1)*24*60 DIFFINMINUTES
FROM
(
SELECT
TO_DATE('2014-06-06 10:30:00', 'YYYY-MM-DD HH24:MI:SS') DATE1,
TO_DATE('2014-06-06 11:25', 'YYYY-MM-DD HH24:MI') DATE2
FROM DUAL
);
The expected result should be 55 minutes but I'm getting 54,99999999.
ROUNDing this value gets the job done but I really wants to understand why this calculation was this behavior.
DATE2 - DATE1 Oracle calculates the difference in days. That's why some inaccuracy may occur.
Just an example with timestamps
select inter,
extract (day from inter) days,
extract (hour from inter) hours,
extract (minute from inter) minutes,
extract (second from inter) seconds
from
(select
cast(date2 as timestamp) - cast(date1 as timestamp) inter
FROM
(
SELECT
TO_DATE('2014-06-06 10:30:00', 'YYYY-MM-DD HH24:MI:SS') DATE1,
to_date('2014-06-06 11:25', 'YYYY-MM-DD HH24:MI') date2
from dual
));
Substraction of timestamps gives you exact INTERVAL

Resources