Oracle date between and? - sql

Why is the results not include '2013-10-14'
select *
from table t
where t.f_date between to_date('2013-10-20', 'yyyy-mm-dd') and
to_date('2013-10-14', 'yyyy-mm-dd')

Does d.f_date include a time component? If it does, the only records that will be returned by between will be those whose time is exactly 00:00:00. All others will be greater that 2013-10-14.
When comparing dates, a date without a time component is assumed to have a 00:00:00 time component

This could be caused by the time component issue mentioned in other answers, however you will also need to change your between to have the earliest date first, otherwise it will never return anything.
e.g.
SELECT *
FROM dual
WHERE SYSDATE BETWEEN SYSDATE AND SYSDATE + 1;
will return a 1 record, but
SELECT *
FROM dual
WHERE SYSDATE BETWEEN SYSDATE + 1 AND SYSDATE;
will not.

Related

ORACE How to get begin of the day from CURRENT_TIMESTAMP

I have a problem with solving this problem. I have to extract the IDs of data from a table that has been changed only today. My current solution is
SELECT DISTINCT id
FROM changes_table
WHERE valid_to > (SELECT TO_DATE(CURRENT_DATE, 'DD-MON-RR') FROM DUAL)
This works in BDeaver but not in my application, there I get "ORA-01843:"
So is it possible to use CURRENT_TIMESTAMP, but it take timestamp from now. Is it possible to change it to format DD-MON-RR with 00:00:00 time?
Or is there any other solution?
I can send only one SQL command.
You may simply use SYSDATE here truncated to midnight:
SELECT DISTINCT id
FROM changes_table
WHERE valid_to > TRUNC(SYSDATE); -- RHS is today at midnight
Note that the above should work whether valid_to be a date or a timestamp.

Sysdate in where clause not working in oracle sql

I have below select query where i am trying to get the data only for today date but its not returning anything:
select * from V_TER
where SYSTEM_INSERTED_AT = SYSDATE;
The SYSTEM_INSERTED_DATE is of Date datatype and the value is stored in this fields as for example 2021-01-15 15:17:13
The problem in Oracle is that dates can have time components both in the data and sysdate itself.
I would recommend checking for any time on the current date:
where system_inserted_at >= trunc(sysdate) and
system_inserted_at < trunc(sysdate) + interval '1' day
This is generally optimizer-friendly. If you don't care about that, then:
where trunc(system_inserted_at) = trunc(sysdate)

Get Records By Todays Date And Before Current Time

I am trying to query some records and am looking to return only records that have a date of today, but also are before the current time.
I have the first part sorted out by using the following clause to return only records for today.
WHERE TRUNC(my_date_time) = TRUNC(sysdate)
How would I modify this to only get records before the current system time as well?
How about inequalities?
WHERE my_date_time >= TRUNC(sysdate) AND
my_date_time < sysdate
Try this.
Select * from table Where
TRUNC(my_date_time) =
TRUNC(sysdate) And
TO_CHAR(my_date_time,
'HH24:MI:SS' ) <=
TO_CHAR(sysdate,
' HH24:MI:SS' )
If column "my_date_time" is of type DATE, and need compare using SYSDATE function then you only need:
WHERE my_date_time<=sysdate
If column "my_date_time" is of type DATE, and need compare with other variable or column of type DATE for example named "other_date"
where my_date_time>=trunc(other_date)
and my_date_time<trunc(other_date)+1

How to retrieve the records based on a date from oracle database

I have a table with date column in it. I need to fetch the records from it based on
the given date.
Currently when i used the query:
select * from workingemployee_data where created_date like '20-Jan-2012'
I am getting those records which have created_date on 20-Jan-2012
But i want to get the records those were created 10 days earlier to a given
date (i.e) 20-Jan-2012.
Please suggest me on this.
This gives all records between today and 10 days ago:
SELECT *
FROM workingemployee
WHERE created_date BETWEEN sysdate - INTERVAL '10' DAY
AND sysdate
This gives all records entered exactly 10 days ago:
SELECT *
FROM workingemployee
WHERE created_date = sysdate - INTERVAL '10' DAY
Replace sysdate with exact date if you want.
Why do you use like and not = ?
Assuming that created_date is of type DATE, it's bad practice to rely on implicit conversion according to NLS_DATE_FORMAT (this is what happens when you compare a date and a string)
dd-mon-yyyy isn't a good format for querying since it deffers according to NLS_LANGUAGE better use mm for months numbers
So, either use #mvp's answer or do something like this:
SELECT *
FROM workingemployee
WHERE trunc(created_date) = to_date('20-01-2013', 'dd-mm-yyyy') - 10
SELECT *
FROM workingemployee
WHERE created_date > sysdate - INTERVAL '10' DAY;

Date difference get different results from function than from select statement

I need to get the difference of 2 date fields, if the greater date is null then I'll use SYSDATE instead. Having this requirement, I created a function to solve this issues (note: this code follows the standard of the organization, not my personal taste)
CREATE FUNCTION F_GET_DIFFERENCE (P_WORKFLOWID NUMBER)
RETURN NUMBER --result in minutes
IS
TIME NUMBER;
BEGIN
TIME := 0
SELECT
F_WORKTIME_DIFF(NVL(X.ENDDATE, SYSDATE), X.STARTDATE)
INTO
TIME
FROM
TABLEX X
WHERE
X.WORKFLOWID = P_WORKFLOWID;
RETURN TIME;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END;
The F_WORKTIME_DIFF function already exists and calculates the worktime of the day (assumming nobody works at 12 a.m. and things like that). The problem is when calling this function, the result contains an additional amount of time. That's very strange, because when executing the query in the function, it returns the expected output.
Example (important: date format in Peru is DD/MM/YYYY HH24:MI:SS)
TABLEX
WORKFLOWID STARTDATE ENDDATE
1 '01/12/2012 10:00:00' null
Assumming that the server day is the same day (01/12/2012) but greater time (10:01:00), we execute the function:
SELECT F_GET_DIFFERENCE(1)
FROM DUAL;
The result is: 14.
Now, executing the query in the function and having the server time at 10:02:00, the result is 2 (exact output).
I even tried executing this
SELECT
F_WORKTIME_DIFF(NVL(X.ENDDATE, SYSDATE), X.STARTDATE) SELECT_WAY,
F_GET_DIFFERENCE(1) FUNCTION_WAY
FROM
TABLEX X
WHERE
X.WORKFLOWID = 1
And the result is (having the server time at 10:10:00)
SELECT_WAY FUNCTION_WAY
10 24
Is maybe any consideration that I must take into account when working with Oracle dates in inner functions or anything that could explain this odd behavior?
It is difficult to tell anything without seeing the function F_WORKTIME_DIFF.
Whatever is the datatype returned from F_WORKTIME_DIFF, it is casted to number when assigned to the variable time. This may be a clue.
This may not be exactly what are you looking for but the first example gives you hours diff between two dates:
Select EXTRACT(HOUR FROM (SYSDATE - trunc(SYSDATE )) DAY TO SECOND ) From dual
/
Select
EXTRACT(hour From Cast(SYSDATE as timestamp)) hh,
EXTRACT(minute From Cast(SYSDATE as timestamp)) mi,
EXTRACT(second From Cast(SYSDATE as timestamp)) ss
From dual
/