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.
Related
If applied to today, I want to return values for 1/10/19 through 25/02/20.
I am using the below currently, but wish to replace it with something that gives me 4 complete months and the current partial month
where DATEDIFF (day,[SubOrderCompletionDate],GETDATE()) between 0 and 180
Thanks
If you want complete months, just change this to:
where DATEDIFF(month, SubOrderCompletionDate, GETDATE()) between 0 and 3
DATEDIFF() counts the number of boundaries between two dates. So, all days within a given calendar month return the same value.
I've taken your statement literally, and therefore assumed that if today was 29 February 2020 you would want 1 November 2019 to 29 February 2020, not 1 October 2019 to 29 Feburary 2020:
SELECT * --This should be a column list, not *
FROM dbo.YourTable YT
WHERE YT.SubOrderCompletionDate >= CASE WHEN EOMONTH(GETDATE()) = CONVERT(date,GETDATE()) THEN DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 3,0) ELSE DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 4,0) END
AND YT.SubOrderCompletionDate <= GETDATE()
If that isn't the case, you just need DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 4,0), which will give you 5 months worth of data.
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()))
)
I have a table X which holds data for each day for a brand. The data for each day is cumulative i.e. sales data for 3 will have data for 1, 2 and 3. Thus data for the last day of each month will be the sales for that month for that brand and company. I want to get the sum of all the sales for that brand for the last 3 months excluding the current month on the last day of each month.
i.e for March: I want sales from 31st Jan 2019 + 28th Feb 2019 + 31st Dec 2018 for each brand and company.
How can I achieve this?
if you are using MSSQL you can use EOMONTH function, example is as under
DECLARE #date VARCHAR(255) = '2/24/2019';
SELECT EOMONTH ( #date ) AS Result;
for MySQL you can use LAST_DAY function
SELECT LAST_DAY('2019-02-24');
Let's say name of your column representing the sales date is "sales_date", then the following predicate will give you the days you're interested in:
sales_date in (
dateadd(day, -1, dateadd(month, datediff(month, 0, getdate()) - 2, 0)),
dateadd(day, -1, dateadd(month, datediff(month, 0, getdate()) - 1, 0)),
dateadd(day, -1, dateadd(month, datediff(month, 0, getdate()), 0))
)
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))
Getting the last 12 months from a specific date is easy and can be retrieved by the following command in SQL-server. Its answer is 2014-08-17.
select Dateadd(Month, -12, '2015-08-17')
What I want is to get the last 12 months but ending at 2014-08-01 (in the above case) instead of any where in the middle of the month.
SELECT dateadd(month,datediff(month,0,getdate())-12,0)
Result is
-----------------------
2014-08-01 00:00:00.000
So the where clause should be
WHERE datecol >=dateadd(month,datediff(month,0,getdate())-12,0)
to get all data starting from jan 01 of last year's same month
Using DATEADD and DATEDIFF:
DECLARE #ThisDate DATE = '20150817'
SELECT DATEADD(YEAR, -1, DATEADD(MONTH, DATEDIFF(MONTH, '19000101', #ThisDate), '19000101'))
For more common date routines, see this article by Lynn Pettis.
To use in your WHERE clause:
DECLARE #ThisDate DATE = '20150817'
SELECT *
FROM <your_table>
WHERE
<date_column> >= DATEADD(YEAR, -1, DATEADD(MONTH, DATEDIFF(MONTH, '19000101', #ThisDate), '19000101'))
If you want all the records since the first day of the current month last year, then you can use:
where <somedate> >= dateadd(day, 1 - day(dateadd(month, -12, getdate()),
dateadd(month, -12, getdate()))
For all days except Feb 29th, you can use the simpler:
where <somedate> >= dateadd(day, 1 - day(getdate()),
dateadd(month, -12, getdate))