Oracle SYSDATE-1 not returning 24 hour range - oracle

I have a table with a timestamp column. My query has a WHERE clause
AND date_created.TIMESTAMP_DATA >= (SYSDATE - 1)
This should return any timestamp which is now-24 hours but I do not get records done the previous day, only the day of (today). I checked that the db SYSDATE is accurate and manipulating date_created to today returns the results, however if date_create is yesterday #5pm, it does not return

Please try changing query as:
AND date_created.TIMESTAMP_DATA >= (SYSTIMESTAMP - 1)
Abhi

Turned out to be a timezone issue. this solved it
AND date_created.TIMESTAMP_DATA >= (SYSTIMESTAMP AT TIME ZONE 'PST'- INTERVAL '1' DAY)
Edited to include - INTERVAL '1' DAY instead of -1

Related

Oracle Time Range Query

I need to write a query which will do a date lookup by the most completed quarter hour.
So, if the current time is 5:35, then the criteria would be 5:15 - 5:30. If the time is 5:46, then 5:30 - 5:45, if the time is 6:02, then 5:45 - 6:00.
Not sure how to easily do this.
Something like
with cqh (dt) as (
select trunc(sysdate, 'hh') +
trunc(extract(minute from systimestamp) / 15) * interval '15' minute
from dual
)
select [_data_] from [_your_table_] cross join cqh
where [_date_column_] >= cqh.dt - interval '15' minute
and [_date_column_] < cqh.dt
;
The subquery calculates the most recently completed quarter-hour. It first truncates SYSDATE to the beginning of the current hour. Then we add a multiple of 15 minutes - the multiplier is 0, 1, 2 or 3, depending on the minute component of SYSDATE. Alas, EXTRACT(MINUTE FROM ...) only works on timestamps, so I had to use SYSTIMESTAMP there instead of SYSDATE, but other than that, the computation should be pretty obvious.
Then cross-join whatever else gives you "the data" to this small helper view, to use the DT value calculated in it.

Oracle - trunc(DateTime) between trunc(Sysdate - 104) and trunc(Sysdate - 75)

I want to select few data from a Oracle table of June month(June 1st to June 30th of 2017) and the start date will be from tomorrow(Sep 13th). Hence I wrote a query in below format,
select * from table where column1='Data1' and Column2='Data2'
and trunc(DateTime) between trunc(sysdate-104) and trunc(sysdate-75)
I'm not able to check this query as I don't have tool for this. I just wrote it in notepad and want to share to my friend.
My Question - Will trunc(DateTime) between trunc(sysdate-104) and trunc(sysdate-75) condition will give data between June1st to June31 or Does any Syntax issue there?
There is no problem with the syntax itself even though your formulation is time sensitive, which means that tomorrow it won't return the same result.
Instead, go with something like this :
AND TRUNC(DateTime) BETWEEN to_date('2016-06-01','YYYY-MM-DD')
AND to_date('2016-06-30','YYYY-MM-DD')
Just cast the date format to month/year and compare against that.
select *
from table
where column1 = 'Data1'
and column2 = 'Data2'
and to_char(DateTime, 'MMYYYY') = '062017';
Hi I think the most accurate would be:
select * from table where column1='Data1' and Column2='Data2'
AND DateTime BETWEEN TRUNC(sysdate-104)
AND TRUNC(sysdate-75)
+ interval '23' hour
+ interval '59' minute
+ interval '59' second;

Is there any better way to get data between days in H2 Database?

We want to select data from table with below condition.
Date of Transactiontime <= (Current Date - n Days)
for e.g.
Today is - 2016-06-21.
Date of Transactiontime = '2016-06-19 11:45:07.148'.
With below query we could get Data which is 2 days older.
Query:
SELECT * FROM T WHERE FORMATDATETIME (Transactiontime,'YYYY-MM-d') <= FORMATDATETIME ( DATEADD('HH',-2*24,Now()), 'YYYY-MM-d');
Dataype of Transactiontime = TIMESTAMP
Is there any better way to get the same results?
Have you tried function DATEDIFF()?
SELECT * FROM T WHERE DATEDIFF('DAY', NOW(), Transactiontime) >= 2
EDIT
Might be better using DAY_OF_YEAR instead of DAY.

Oracle ORA-01839: date not valid for month specified Leap Year

Oracle 11g
here is a quick one hopefully.
Below is part of a script that gets date only from from the next month
first day of next month to last day. But today 29th feb it thrown an error of
ORA-01839: date not valid for month specified
M.MS_DATE between trunc(sysdate + interval '1' month,'MM') and last_day(sysdate + interval '1' month)
Is there a way round this. Many thanks
I have seen this as well and I consider this a bug in Oracle.
The workaround is to use add_months() instead :
between trunc(add_months(sysdate,1),'MM') and last_day(add_months(sysdate,1));
I would probably use add_months() as a_horse_with_no_name suggests, but just as an alternative if you want to use intervals, you can move the point you do the truncation in the first expression, and include the same truncation in the second expression:
select trunc(sysdate, 'MM') + interval '1' month as first_day,
last_day(trunc(sysdate, 'MM') + interval '1' month) as last_day
from dual;
FIRST_DAY LAST_DAY
---------- ----------
2015-02-01 2015-02-28
This works because all months have a first day, so you don't trip over the documented caveat.
When interval calculations return a datetime value, the result must be an actual datetime value or the database returns an error

How to display data by the current system date

I have tried this, but it's not working:
SELECT day FROM table
WHERE (SYSDATE - day) *24 < 0.5;
How can I get data in my database with its current date?
Assuming that day is a date column, why not do:
select day
from table
where trunc(day) = trunc(sysdate);

Resources