Retrieve a date record which is with in a 30 days range by given date SQL - 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)

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

Need to get data between two dates in 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;

Getting interval Monday-Sunday in SQL BIGQUERY

I'm trying to get the data between last weeks monday and last week sunday. I'm having trouble with getting the relative part. I'm trying like this:
where date <= LASTWEEKSUNDAY OR date >= LASTWEEKMON
The closest I got to what I seek was using now(), but it returned also some days from the current week. Thanks in advance
You are describing:
where date >= date_sub(date_trunc(current_date, week(Monday), interval 1 week) and
date < date_trunc(current_date, week(Monday))
Although the function calls change, the same logic works on datetimes and timestamps.
Of course week(Monday) is the default for isoweek, so you can use:
where date >= date_sub(date_trunc(current_date, isoweek, interval 1 week) and
date < date_trunc(current_date, isoweek)
I think it's what you want
where date between DATE_SUB(DATE_TRUNC(CURRENT_DATE(), WEEK(SUNDAY)), interval 6 day) and DATE_TRUNC(CURRENT_DATE(), WEEK(SUNDAY))
You shouldn't use OR in the where statement, it'll cover all the days if you use OR. Instead, you can prefer using AND or between.

count the number of days of current month from day 1 until yesterday

I am trying to calculate the number of days of the current of month from day 1 until yesterday without the need of changing the count manually. The original SQL as below:
select order_id
from orders
where date > dateadd(-23 to current_date) and date < 'today'
the desired code is something like
select order_id
from orders
where date > dateadd(datediff(day,firstdayofthemonth,current_date) to current_date) and date < 'today'
Appreciate any help
In firebird you could do:
WHERE
date >= DATEADD(1 - EXTRACT(DAY FROM CURRENT_DATE) DAY TO CURRENT_DATE)
AND date < CURRENT_DATE
In addition to the answer provided by Mark, you can also use BETWEEN (starting with Firebird 2.0.4)
WHERE
date BETWEEN current_date - extract(day from current_date) + 1
AND current_date - 1
P.S. all those answers rely upon DATE data type (thus, date column and CURRENT_DATE variable) having no time part. Which is given for modern SQL dialect 3. But if Dialect 1 would get used it is not given.
https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-commons-predicates.html
https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-background.html#fblangref25-structure-dialects
In addition to the answer provided by GMB, you can also use fact that Firebird allows addition of days to a date without needing to use dateadd:
date > current_date - extract(day from current_date)
and date < current_date

Is 'between' which includes START and END date?

I have to query 10 days data from snowflake database. I tried the date between '2019-07-30' and '2019-08-09' which includes start and end date?
Dates should be in single quotes as such:
Date >= '2019-07-30' and Date <= '2019-08-09'
You can also use between as you mentioned:
Date between '2019-07-30' and '2019-08-09'
Adding this into your WHERE clause will seperate results between these two dates
If you want to include all dates including today going back 9 days (so 10 total days) then try using this WHERE clause:
WHERE date >= DATEADD(DAY, -9, CURRENT_DATE()) AND
date < DATEADD(DAY, 1, CURRENT_DATE())
This says to match dates which occur on or after midnight of 9 days ago, up until any date strictly before midnight of tomorrow (implying all of today matches).
Here's the BETWEEN Snowflake documentation, and it reads:
"The expression A BETWEEN X AND Y is equivalent to A >= X AND A <= Y"
use below Query
SELECT * FROM snowflake WHERE start_date >='2019-07-30' and end_date <= '2019-08-09';