I have a database with a date column that logs when a new "contact" is created. The contacts are generated when the call center receives a new call or e-mail.
What I want is for the where clause to capture the last 13 months of full data.
Examples:
Today is 1/30/2015, if executed the query would return records from 12/1/2013 to 12/31/2014.
Today is 2/06/2015, if executed the query would return records from 1/1/2014 to 1/31/2015.
The query will include those dates falling on the first and last days of the month.
The code I have is as follows:
WHERE
dbo.ub_contact.contact_dt BETWEEN DATEADD(year, -1, (DATEADD(month, DATEDIFF(month, -1, getdate()) - 1, -1) + 1))
AND DATEADD(month, DATEDIFF(month, -1, getdate()) - 1, -1)
Ran today (1/30/2015) this code seems to be returning 1/1/2014 - 12/31/2014.
I would appreciate any help toward getting this worked out.
Thanks!
John
Use this dates:
SELECT EOMONTH(DATEADD(mm, -1, GETDATE()))
SELECT DATEADD(dd, 1, EOMONTH(DATEADD(mm, -14, GETDATE())))
So you where clause would look like:
WHERE dbo.ub_contact.contact_dt BETWEEN DATEADD(dd, 1, EOMONTH(DATEADD(mm, -14, GETDATE()))) AND EOMONTH(DATEADD(mm, -1, GETDATE()))
Related
I'm currently building a query which needs to show data from Saturday to Friday. For example, running the query today you should get data from 7/24 to 7/30. I’m seeing data from 7/25 to 7/30 and I frankly don’t know how to fix the code below to get Saturday’s data as well.
Here’s what I have:
WHERE
InvoiceDate BETWEEN DATEADD(WEEK, -2, DATEADD(WEEK, DATEDIFF(WEEK, 5, GETDATE()), 5))
AND DATEADD(DAY, -3, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0))
Preferably, I’d like to keep the similar format rather than parameterizing the query. Unless that’s a better idea of course! I’m also asking this on my phone so I apologize for not including the entire query. However because it is in the where clause, I figured the rest wasn’t needed.
you're leaving out a lot of information so assuming that you are running this query every Monday (as of my reply, today is Monday) and you are just going back to last-last Saturday to last Friday, your WHERE clause can be quite simple:
where InvoiceDate between dateadd(day,-9,getdate()) and dateadd(day,-3,getdate())
if there are are more criteria or conditions then let us know.
try the following query, just put the date whenever you need
SELECT
case
when datepart(weekday, '2021/08/03') >5
then DATEADD(DAY, +4, DATEADD(WEEK, DATEDIFF(WEEK, 0, '2021/08/03'), 0))
else DATEADD(DAY, -9, DATEADD(WEEK, DATEDIFF(WEEK, 0, '2021/08/03'), 0))
end AS last_saturday,
case
when datepart(weekday, '2021/08/03') >5
then DATEADD(DAY, +4, DATEADD(WEEK, DATEDIFF(WEEK, 0, '2021/08/03'), 0))
else DATEADD(DAY, -3, DATEADD(WEEK, DATEDIFF(WEEK, 0, '2021/08/03'), 0))
end AS last_friday;
I realize you have an answer already, but this will allow you to use whatever date you want. Using the day of the week to subtract days from today will give you the previous Saturday. Subtracting 7 from that will give you the Saturday a week ago.
So given that, the below will search the range from the start of a week ago Saturday until the start of this previous Saturday. This works with both date and datetime data types.
WHERE InvoiceDate >= DATEADD(day, -7 + DATEPART(dw,GETDATE()) * -1, GETDATE())
AND InvoiceDate < DATEADD(day, DATEPART(dw,GETDATE()) * -1, GETDATE())
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 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))
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.
When I run this where clause on my table I get 2 different results and to me its seems like I should get the same number of records back.
The one I'm using just a static date to test and the other should also retrieve the same results where I'm trying to get last month results
I idea the query is a report that will automatic load the previous months records.
WHERE
(OrderReceiptedDate >= '2015-03-01')
AND (OrderReceiptedDate <= '2015-03-31')
WHERE
(DATEPART(mm, OrderReceiptedDate) = DATEPART(mm, DATEADD(mm, - 1, GETDATE())))
AND
(DATEPART(yy, OrderReceiptedDate) = DATEPART(yy, DATEADD(mm, - 1, GETDATE())))
These are the two statements
WHERE (OrderReceiptedDate >= '2015-03-01' AND
OrderReceiptedDate <= '2015-03-31'
)
WHERE (DATEPART(month, OrderReceiptedDate) = DATEPART(month, DATEADD(month, - 1, GETDATE()))) AND
(DATEPART(year, OrderReceiptedDate) = DATEPART(year, DATEADD(month, - 1, GETDATE())))
Given that today is April 2015, you are expecting that both of these get all dates for March. And, they would, if your dates had no time components. The problem is that almost any datetime on March 31st is not going to match the first condition. The one exception is exactly at midnight: 2015-03-01 00:00:00.000.
The first is better written as:
WHERE (OrderReceiptedDate >= '2015-03-01' AND
OrderReceiptedDate < '2015-04-01'
)
A better way to write "get me last months date" is something like:
WHERE OrderReceiptedDate >= dateadd(month, -1, cast(getdate() - day(getdate()) + 1 as date)) and
OrderReceiptedDate < cast(getdate() - day(getdate()) + 1 as date)
This does all the calculations on getdate() so the query could still take advantage of an index on OrderReceiptDate.