I am trying to make a query to get the last 60 days from last year.
I have the current period
DATE >= DATEADD(DAY, -60, current_timestamp) AND PEDDTEMIS < 'TODAY'
What I am trying to get
DATE >= DATEADD(DAY, -60, DATEDIFF(YEAR,current_timestamp,-1) AND DATE < DATEDIFF(YEAR,'TODAY',-1)
The dates I am trying to get:
current: 06/14/2019 - 04/15/2019
last year: 06/14/2018 - 04/15/2018
Appreciate any help
I think you're trying to get the "60 days preceding one year ago today" (though the title of your question suggests something different).
One year ago today should be: DATEADD(-1 YEAR TO CURRENT_DATE)
Sixty days prior should be: DATEADD(-60 DAY TO DATEADD(-1 YEAR TO CURRENT_DATE))
So you are looking for records BETWEEN those two date values.
Looks like you are with Oracle. DATEDIFF returns the difference between two dates in YEARS/DAYS etc. What you need is just DATEADDs to subtract a year and to subtract 60 days:
DATE >= DATEADD(DAY, -60, DATEADD(YEAR,current_timestamp,-1) AND DATE < DATEADD(YEAR,current_timestamp,-1)
Well, this comes to mind:
where date >= '2018-11-02'
You could try
SELECT DATEADD(DAY,-60, DATEADD(DAY, -1, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)))
Related
This question already has answers here:
Query to get content older than 3 weeks
(3 answers)
Closed 2 years ago.
This week is the week of 12/28/20. I want to pull data from the third week back of whatever the current week is. So if I run my query today I want it to pull data for the week of 12/7 only. I'm trying to do this and its not returning any results. Any ideas? Thank you
WHERE date = DATEADD(week, -3, GETDATE())
I prefer the following which:
Allows you to use the Date column without calling a function on it. This is important for sargability i.e. the ability for SQL Server to use an index on the Date column. Although in my testing a cast to date still did you use the index, its best practice to avoid functions calls on the columns in where clauses.
Avoids the use of between, which I always have to take a moment to think about because its >= the first condition and <= the second condition. So its easy to get the logic wrong and include 8 days instead of 7. Instead I always use an explicit compare so its obvious what the logic is.
Note the >= and < now rather than <=.
where [Date] >= convert(date,dateadd(week, -3, current_timestamp))
and [Date] < convert(date,dateadd(week, -2, current_timestamp))
Now
Start Of Week (Inc)
End Of Week (Ex)
2020-12-29 20:07:16.373
2020-12-08
2020-12-15
And if you need it to start on your week start day then use:
where [Date] >= convert(date,dateadd(day, (-1*datepart(weekday,current_timestamp))+1, dateadd(week, -3, current_timestamp)))
and [Date] < convert(date,dateadd(day, (-1*datepart(weekday,current_timestamp))+1, dateadd(week, -2, current_timestamp)))
Now
Start Of Week (Inc)
End Of Week (Ex)
2020-12-29 20:07:16.373
2020-12-06
2020-12-13
That is only going to give you data that has a date that was three weeks ago from the current time to the nearest thousandth of a second. It's fairly unlikely that you have any data that exactly matches that.
What I expect you are after is data that is between three and two weeks ago which can be obtained like this:
WHERE date BETWEEN DATEADD(week, -3, GETDATE()) AND DATEADD(week, -2, GETDATE())
However if you run that half way through a day, it will only give you data from half way through the day three weeks ago until half way through the day two weeks ago. You probably want to go from midnight at the start and end of the day. This can be done by casting everything to the DATE type, which has the effect of discarding the time, like this:
WHERE CAST(date AS DATE) >= CAST(DATEADD(week, -3, GETDATE()) AS DATE) AND CAST(date AS DATE) < CAST(DATEADD(week, -2, GETDATE()) AS DATE)
It is also possible that you want to have the weeks always start on the same day of the week. This can be done using the DATEPART function.
WHERE DATEPART(week, date) = DATEPART(week, DATEADD(week, -3, GETDATE())) AND DATEPART(year, date) = DATEPART(year, DATEADD(week, -3, GETDATE()))
Depending upon what you want and how your database is set up this last option may require setting the SET DATEFIRST setting to get the correct day to start the week on. For example to start the week on Mondays use:
SET DATEFIRST 1;
So what I am trying to do is when I run the query, I want to return all records that were in the month two months from the current month. For example, lets say the current month is November, when the query runs, I want returned all records from September and only September. If I run the query in lets say October, I want all records from August and only August. I am trying to do this in MS SQL. Thanks for any advice.
In SQL Server, you can use:
where datecol >= dateadd(month, -3, datefromparts(year(getdate()), month(getdate()), 1)) and
datecol < dateadd(month, -2, datefromparts(year(getdate()), month(getdate()), 1))
This is index- and optimizer- friendly. If you don't care about performance, you can use datediff():
where datediff(month, datecol, getdate()) = 2
This can be done in a nice 1 liner.
WHERE NOW() BETWEEN Date1 AND Date2;
You can have the month part in a variable and then it can be used in the Where clause to filter the month part of the date value is equal to the varoable value.
Query
Declare #month as int;
Set #month=datepart(month, getdate()) - 2;
Select * from yourTableName
Where month(dateCol) = #month;
The function GETDATE() can be used to retrieve the current month.
The function DATEADD(datepart,number,date) can be used to perform operations on dates. For more info look at the official docs
Thus, to retrieve the records from two months before (-2) the current month you can use the following:
DATEADD(month, -2, GETDATE())
In conclusion an example query to select all records that were in the month two months from the current month:
SELECT * FROM table
WHERE MONTH(month_column) = DATEADD(month, -2, GETDATE())
sources:
WHERE Clause to find all records in a specific month
SQL query for today's date minus two months
I'm using this code in an SQL query
WHERE [Date] >= DATEFROMPARTS(DATEPART(year,GETDATE()),DATEPART(month,GETDATE())-1,DATEPART(day,GETDATE()))
AND [Date] <= EOMONTH(DATEFROMPARTS(DATEPART(year,GETDATE()),DATEPART(month,GETDATE())-1,DATEPART(day,GETDATE())));
The problem is come 2020 the December query will through up an error
The code I posted manages the dates between which data will be returned. It looks at the date the code is run and choose that day from last month till the end of last month. What I need is dates from the 1st till the last day of the month prior to the one this code is called in.
I will be working on this issue tomorrow, it will be interesting to see what solutions other people can come up with.
try this
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0),
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) - 1
it will get you the previous month start and end date
If you want the previous month:
where date >= dateadd(month, -1, datefromparts(year(getdate(), month(getdate(), 1))) and
date < datefromparts(year(getdate(), month(getdate(), 1))
This simply checks that it is before the first of the this month and then subtracts a month from that.
I would like to get last week, last month and last quarter numbers (appended with year number) based on current date.
I have used CONCAT and IIF to get current week/month/quarter numbers and substract it with -1, then check if it is last month/quarter to handle 0 values. Below is the code to get last month and quarter, however I am looking for an optimised code to make it work better. Also getting last week number using code similar to below will have issue with leap/non-leap years.
Last month:
SELECT CONCAT(YEAR(GETDATE()),IIF(DATEPART(MONTH,GETDATE())-1=0,12,DATEPART(MONTH,GETDATE())-1))
Last quarter:
SELECT CONCAT(YEAR(GETDATE()),IIF(DATEPART(QUARTER,GETDATE())-1=0,4,DATEPART(QUARTER,GETDATE())-1))
For example, If my current date is 4th Jan, 2019 -
Last week should return 52 or 53 (based on leap year), Last month should return 12, Last quarter should return 4.
--a week ago
select DATEADD(WEEK, -1, GETUTCDATE())
--the week number(of year), a week ago
select DATEPART(WEEK, DATEADD(WEEK, -1, GETUTCDATE()))
--a month ago
select DATEADD(MONTH, -1, GETUTCDATE())
--the month number, a month ago
select DATEPART(MONTH, DATEADD(MONTH, -1, GETUTCDATE()))
--a quarter ago
select DATEADD(QUARTER, -1, GETUTCDATE())
--the quarter number, a quarter ago
select DATEPART(QUARTER, DATEADD(QUARTER, -1, GETUTCDATE()))
General formula is thus:
Day and time, some PERIOD (week, month, quarter, year etc) ago:
DATEADD(PERIOD_IDENTIFIER, -NUMBER_OF_PERIODS, CURRENT_DATE)
The period that it was then:
DATEPART(PERIOD_IDENTIFIER, DATEADD(PERIOD_IDENTIFIER, -NUMBER_OF_PERIODS, CURRENT_DATE))
ps; every year has 53 weeks, not just leap years, because 365/7 is fractionally over 52
pps; I've used GetUtcDate above because I typically work in UTC as most of my tasks are on multi-country systems and all times are UTC. If you're specifically after something that reports "last X" for your local timezone you might need to use GetDate() instead so that the concept of "this week" and "last week" etc is aligned with your local concept of midnight/day changing
Use this in SQL Server.
select datepart(mm,getdate()-30) as Last_Month
,datepart(qq, getdate()-7) as Last_Quarter
,datepart(wk,getdate()-7) as Last_Week
i want to make a select, where the users birthday(date field) is less than 30 days.
what is the best way to to do it? i tried datediff, but i don't know how to put the year aside.
Thanks
You could just use DATEPART function with dayofyear datepart value.
EDIT: honestly, there is a boundary issue in my previous answer (many thanks to Damien): e.g. 2010-12-25 and 2011-01-07 => the difference should be less then 30 days, but DATEPART(dayofyear, #date) - DATEPART(dayofyear, [Birthday]) < 30 condition would skip this record. So I added an additional contition to my answer:
DATEPART(dy, #d) - DATEPART(dy, [Birthday]) < 30 OR
(
DATEPART(mm, #d) = 12 AND
DATEPART(dy, DATEADD(m, 1, #d)) - DATEPART(dy, DATEADD(m, 1, [Birthday])) < 30
)
it adds one month to the each date in the case when the month part of the first date is December and compares the difference.
A common way is to compose a formatted date, as text, and replace the year with the current year; and parse back into a date. Apply datediff on that.
If you find out datediff returns something negative thus the birthday of this year is in the past, add 1 year, and try again. This is for the time period around New Year.
SELECT *
FROM dbo.CheckBirthDay
WHERE (CASE WHEN YEAR(BirthDay) <= YEAR(CURRENT_TIMESTAMP) THEN DATEDIFF(DD,BirthDay,CURRENT_TIMESTAMP) END < 30 )