Need to get data between two dates in SQL - sql

I need to get data between two dates. here, I have added simple example as below :
Ihave added below logic in my SQL query but not working, pls help me :
like If MyDate = 2020-07-09 15:15:00
I have to run cron job. So, get those type data which datas MyDate between date of 12 hours ago and date of pending 1 hour to complete MyDate.
Pls help me to get idea using Mysql queries.
I tried this one but not getting data :
SELECT * FROM test WHERE ENDDATE BETWEEN (ENDDATE - INTERVAL 12 HOUR) AND (ENDDATE - INTERVAL 1 HOUR) ORDER BY ENDDATE DESC;

look into whereBetween
whereBetween('reservation_from', [$from1, $to1])
https://laravel.com/docs/5.6/queries#where-clauses

Guys, I got the Solution from MyEnd as below :
SELECT * FROM test WHERE ( now() > (ENDDATE - INTERVAL 12 HOUR) AND now() <= (ENDDATE - INTERVAL 1 HOUR)) ORDER BY ENDDATE DESC;

Related

Getting records between current date and next x days in Presto

Hope someone can help me out. I'm trying convert a line of SQL to work with Presto. Currently in SQL I do the following to get all records that are due in the next 0-5 days:
((EventStartDate)between getdate()-1 and dateadd(day, 5, getdate()))
I thought it would be something like this in Presto
EventStartDate between current_date and interval '5' day
But get the following error in AWS Athena: Cannot check if date is BETWEEN date and interval day to second
Thanks,
Mark
Interval needs a date or timestamp to be used and BETWEEN can only be made between to equal entities to dates twp timestamps two numbers
So do this instead
EventStartDate between current_date and current_date + interval '5' day

Get exact results in a QUERY using CAST, BETWEEN and INTERVAL

Below is the query for One week past due
Select count(*) as OneWeekPastDueNC from tblNAME WHERE
ColumnNm = '9940081135'
AND
CAST(DueDt AS DATE) BETWEEN DATE_SUB(DATE "2020-03-19", INTERVAL 7 DAY) AND DATE '2020-03-19'
Getting data results WITH DueDt 2020-03-20. Expecting that my date boundaries is from 2020-03-12 to 2020-03-19 only.
Your question needs a database tag and it should work. However, I would recommend not using the date() function and instead doing direct comparisons:
WHERE ColumnNm = '9940081135' AND
(DueDt >= DATE_SUB(DATE '2020-03-19', INTERVAL 7 DAY) AND
DueDt < DATE '2020-03-20'
)

How to get table data between two time using sql query

i am using this below sql query to get the table data those was updating yesterday between 12:00 AM to 11:59 AM. In this query i need to put date on daily basis but i don't want to put date again and again so i want another query to get table data without updating date.
select *
from transaction_persistence
where currentdatetimestamp between '18-MAY-2017 12.00.00 AM' and '18-MAY-2017 11.59.59 AM';
Use now() or curdate():
select *
from transaction_persistence
where currentdatetimestamp >= CURDATE() and
currentdatetimestamp < CURDATE() + interval 12 hour;
Note: When working with date or date/time values, BETWEEN is dangerous. In your case, you are missing one second of every half day.
EDIT:
You get Oracle errors with Oracle, not MySQL:
select *
from transaction_persistence
where currentdatetimestamp >= trunc(sysdate) and
currentdatetimestamp < trunc(sysdate) + 0.5
Use DATE_SUB() and CURDATE()
SELECT *
FROM transaction_persistence
WHERE currentdatetimestamp<CURDATE() AND currentdatetimestamp>=DATE_SUB(CURDATE(),INTERVAL 1 DAY)

How to find records from yesterdays time till todays time in sql?

I am trying to find records from yesterdays 10:30 PM till today's 10:30 PM with SQL query. Please help me with sql query to find such records.
Maybe its a duplicate question, if so please link me to that. Don't want any pl-sql function.
A simple way to do this is to subtract times and compare dates. So, one way is:
select t.*
from t
where trunc(datecol) = trunc(sysdate - 1.5/24);
It is more efficient to use a direct comparison (because Oracle can more readily use an index):
select t.*
from t
where datecol >= trunc(sysdate) - 1.5/24 and
datecol < trunc(sysdate) + 1 - 1.5/24;
Note: You can also use interval for this purpose, if you are less old-fashioned than I am:
select t.*
from t
where datecol >= trunc(sysdate) - interval '90' minute
datecol < trunc(sysdate) + interval '1' day - interval '90' minute;
You can get the yesterday date with SYSDATE - 1. You would need something like this:
SELECT ...
FROM ...
WHERE date_field BETWEEN SYSDATE-1 AND SYSDATE

Retrieve a date record which is with in a 30 days range by given date SQL

I am having trouble to find the records which are with in a 30 days range in a given date
Ex: I have record with the date of 10/31/2014 and I have a given specific Due date of 11/28/2014.
I would like to retrieve this record(10/31/2014) when my current date is 10/28/2014 until my current date becomes 11/28/2014 (i.e with in 30 days range). If my current date is 11/29/2014 then I no need retrieve this record.
I have spend almost 3 hours of my time. It will be greatly appreciated if you can give me a query for it.
Thanks,
VJ.
The general format is something to the effect of:
where duedate >= CURRENT_DATE - interval '30' day and duedate <= CURRENT_DATE
This is standard syntax and will work in MySQL and Postgres. An Oracle equivalent is:
where duedate >= trunc(sysdate) - 30 day and duedate <= trunc(sysdate)
And a SQL Server equivalent is:
where duedate >= cast(getdate() - 30 as date) and duedate <= cast(getdate() as date)