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

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.

Related

Operator BETWEEN and dates in Oracle NoSQL Database, how to do?

I want to do a query in nosql database using the BETWEEN operation ? I want to do something like this:
SELECT * FROM table
WHERE column_timestamp BETWEEN '2021-01-01'
AND '2021-12-31'
How to do this query in NoSQL Database? Is BETWEEN supported ?
In your case because your column seems to be a timestamp, you need to cast the timestamp values to TIMESTAMP type. Then you'd just use <= and >= operators.
Be careful, when doing queries with only dates w/o providing the time. Here a testcase using <, <=, >=, >.
I've inserted 3 rows :
2021-12-01T00:00:00Z
2021-12-31T00:00:00Z
2021-12-31T23:59:59Z
SELECT * FROM TEST
where date > CAST("2021-12-01" as timestamp) and date < CAST("2021-12-31" as timestamp)
no rows selected
SELECT * FROM TEST
where date >= CAST("2021-12-01" as timestamp) and date <= CAST("2021-12-31" as timestamp)
2 rows : 2021-12-01T00:00:00Z and 2021-12-31T00:00:00Z
SELECT * FROM TEST
where date >= CAST("2021-12-01T00:00:00" as timestamp)and date <= CAST("2021-12-31T23:59:59" as timestamp)
3 rows : 2021-12-01T00:00:00Z , 2021-12-31T00:00:00Z and 2021-12-31T23:59:59Z
SELECT * FROM TEST
where date >= CAST("2021-12-01" as timestamp) and date < CAST("2022-01-01" as timestamp)
3 rows : 2021-12-01T00:00:00Z , 2021-12-31T00:00:00Z and 2021-12-31T23:59:59Z

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;

Oracle SYSDATE-1 not returning 24 hour range

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

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);

Oracle - Another way for month query?

first... sorry for my english.
I have a query like this:
Select *
From tableA
Where (
TO_NUMBER(TO_CHAR(dateA(+),'SYYYY')) = 2013
AND TO_NUMBER(TO_CHAR(dateA(+),'MM')) = 02
AND to_number(to_char(dateA(+),'dd')) <= 25
)
and retrieve me the data from each date until last number that I give as parameter, in this case the day 25. This working but delay very much because the form of "Where" statement... anybody know another way that retrieve the data so fast and with the same functionality?
It sounds like you want
SELECT *
FROM tableA
WHERE dateA BETWEEN trunc( date '2013-02-26', 'MM' ) AND date '2013-02-26'
This will return all the rows where dateA is between the first of the month and the specified date. If there is an index on dateA, Oracle would be able to use it for this sort of query (though whether it actually would is a separate issue).

Resources