picking out data from 2 months ago only - sql

I'm currently using this which gives me all the data from the previous month. So if i run it today in November, i get all of Octobers data.
Now i need to tweak this so it only shows data in September, and when we're in December will only show October etc
[DateTimeOfCall] between DATEADD(month, datediff(month, 0, getdate())-1, 0) and DATEADD(month, datediff(month, 0, getdate())+0, 0)
Thank you

My guess would be
[DateTimeOfCall] between DATEADD(month, datediff(month, 0, getdate())-2, 0)
and DATEADD(month, datediff(month, 0, getdate())-1, 0)

SELECT * FROM table1
WHERE mydate BETWEEN DATE_SUB(now(), INTERVAL 3 MONTH) AND now()

Related

Get records of last 2 months (current year), and last month(last year)

I need to get records of last 2 months and last month(last year) based on my table field paidDate, using SQL server 2016.
Suppose, I run the query on Feb 1st/2nd, 2020. I need the monthly data from December 2019, January 2020, as well as January 2019.
What's the SQL query for this? Is it possible to club all of these scenario into one?
Then for the previous 2 months the paidDate would be :
A) Higher or equal than the first day of 2 months ago
B) Lower than the first day of the current month.
Similar for the month of a year ago.
So try something like this:
SELECT *
FROM YourTable
WHERE
(
paidDate >= DATEADD(month, -2, DATEADD(month, DATEDIFF(month, 0, GetDate()), 0))
AND paidDate < DATEADD(month, DATEDIFF(month, 0, GetDate()), 0)
)
OR
(
paidDate >= DATEADD(month, -13, DATEADD(month, DATEDIFF(month, 0, GetDate()), 0))
AND paidDate < DATEADD(year, -1, DATEADD(month, DATEDIFF(month, 0, GetDate()), 0))
)
LukStorm has the better answer in terms of performance (and I've upvoted it). But if you want complete months and don't care about indexing, then I would suggest datediff():
where datediff(month, paiddate, getdate()) in (1, 2, 13)
This gets the complete months that are 1 month, 2 months, and 13 months in the past.
You can try the logic as below-
SELECT *
FROM your_table
WHERE
(
YEAR(paidDate) = YEAR(DATEADD(MM,-1, getdate()))
AND
MONTH(paidDate) = MONTH(DATEADD(MM,-1, getdate()))
)
OR
(
YEAR(paidDate) = YEAR(DATEADD(MM,-2, getdate()))
AND
MONTH(paidDate) = MONTH(DATEADD(MM,-2, getdate()))
)
OR
(
YEAR(paidDate) = YEAR(DATEADD(MM,-13, getdate()))
AND
MONTH(paidDate) = MONTH(DATEADD(MM,-13, getdate()))
)

Retrieving last months data when last month is also last year?

I have the following Where clause in several queries. This successfully retrieves the past months data. Now the year has changed, the query can't find any data (December 2018 hasn't happened yet!). How can I change the Where clause to overcome this?
select *
from somedatabase a
WHERE DATEPART(m, a.meetDate) = DATEPART(m, DATEADD(m, -1, getdate()))
and DATEPART(yyyy, a.meetDate) = DATEPART(yyyy, getdate())
Many thanks and any assistance very gratefully received.
My normal way of rounding down to the start of the current month is:
DATEADD(month, DATEDIFF(month, 0, getDate()), 0)
Find out how many whole months there have been since date 0
Then add that many months to date 0
Always gives the start of the month (as date 0 is the start of a month)
Is not affected by leap year, year boundaries, months of various length, etc
This then allows me to do things like...
WHERE
a.meetDate >= DATEADD(month, DATEDIFF(month, 0, getDate()) - 1, 0) -- start of last month
AND a.meetDate < DATEADD(month, DATEDIFF(month, 0, getDate()) , 0) -- start of this month
By having the calculations on the right hand side you make maximum use of indexes.
Here is one way:
WHERE DATEPART(month, a.meetDate) = DATEPART(month, DATEADD(m, -1, getdate())) AND
DATEPART(year, a.meetDate) = DATEPART(year, DATEADD(m, -1, getdate()))
That is, subtract one month for both comparisons.
Actually, a simpler way is to use the strange rules of DATE_DIFF():
WHERE DATEDIFF(month, a.meetDate, getdate()) = 1
Neither of these can make use of an index. For that, the expression is a little more complicated:
WHERE a.meetDate >= DATEFROMPARTS(YEAR(DATEADD(MONTH, -1, GETDATE()),
MONTH(DATEADD(MONTH, -1, GETDATE()),
1) AND
a.meetDate < DATEADD(DAY, 1 - DAY(GETDATE()), CAST(GETDATE() as DATE))

SQL to return only previous full months

In my table i have a date column called APPDATE, i would like to use this to restrict a query to return only the last 2 complete months.
For example, today is 28/03/17, i would like the query to only return data from February 2017 and January 2017 and not to include any data from March 2017.
How would i do this please?
At the moment I've tried:
APPDATE > DATEADD(MONTH, -2, GETDATE())
which includes March :(
Try the following WHERE clause:
WHERE APPDATE < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AND
APPDATE >= DATEADD(MONTH, -2, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
Assuming the current date is in March, then this logic would retain all records earlier than March 1 and greater than or equal to January 1.

sql get data where date is greater than current month

i want to retrieve records where a date field is set to future months
does this look correct
Select * from table1 WHERE
datesetto >MONTH(dateadd(dd, -1, GetDate())))
select * from tablename where month(columndate)>month(getdate()) and
year(columndate)>=year(getdate())
SELECT * FROM table1
WHERE datesetto >= DATEADD(month, DATEDIFF(month, 0, getdate())+1, 0)
Explanation:
DATEDIFF(month, 0, getdate()) calculates how many months have passed since 1900-01-01.
DATEADD(month, DATEDIFF(month, 0, getdate()), 0) returns the beginning of this month.
DATEADD(month, DATEDIFF(month, 0, getdate())+1, 0) returns the beginning of next month.
Try this:
Select * from table1 WHERE
((DATEPART(MONTH,datesetto) > DATEPART(MONTH,GETDATE())
AND DATEPART(YEAR,datesetto) = DATEPART(YEAR,GETDATE()))
OR (DATEPART(YEAR,datesetto) > DATEPART(YEAR,GETDATE())))
DATEPART(Month,GETDATE()) will give the month of the current date and then you can compare it with the datesetto
Update: The above query will give data for any months greater than the current month and any month greater in the year than the current year.

6 month rolling data

This query is currently pulling back 6 month data, so if I run it today I get data from 01/03/2016 to 06/09/2016. What I would like is the data to stop at 31/08/2016.
And then the following month start at 01/04/2016 to 30/09/2016 and so on.
Many thanks
select i.Date
from table as i
where i.Date >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0)
order by i.Date desc
Try :
select i.Date
from table as i
where i.Date between
Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0)
AND DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, 0)
order by i.Date desc