SQL update date to first of next month plus three months - sql

I have this query:
update courseRights
set courseRightsLevelExpires = DATEADD(MM,3,courseRightsLevelExpires)
This works fine but what I actually need is to extend to three months from the first day of next month. For example if the rights are expiring today, May 23rd, I need to update to June 1st + 3 months.
Is it possible to do that in one query?
Update
Because it was flagged as duplicate with another question, I'm updating the content to say that I'm not only looking for the first date of next month but I need to add three months to that date.

To get the first day of next month:
DATEADD(m, DATEDIFF(m, -1, current_timestamp), 0)
Three month's after that:
DATEADD(m,3,DATEADD(m, DATEDIFF(m, -1, current_timestamp), 0))
So, if courseRightsLevelExpires holds the base date you're working from:
DATEADD(m,3,DATEADD(m, DATEDIFF(m, -1, courseRightsLevelExpires), 0))

You can use eomoth() function :
update courseRights
set courseRightsLevelExpires =
dateadd(mm, 3, dateadd(dd, 1, eomonth(courseRightsLevelExpires)));

I will do:
set nextmonthdate = last_day (your-date) + 1 day

Related

Query to return all values from next month

I have a dataset of subscriptions in a table that looks something like this:
Table:
UserID
Subscription_type
Subscription_end_date
I'm trying to build a report in SSRS that shows a list of all subscriptions that are going to expire somewhere in the next month. The report gets sent on the first of every month and needs to show all the rows that are going to expire in the next month.
For example: a report sent on 01-07-2021 needs to show all subscriptions that are going to expire during August (so all subscription_end_date with a value from 01-08-2021 to 31-08-2021).
At first I thought using a where statement like the below one would be the answer:
WHERE [Subscription_end_date] = DATEADD(MONTH, 1, GETDATE())
But somehow it doesn't return any values. I would like to know if my solution is in the right direction and that DATEADD combined with the GETDATE should do the trick or that I have to search it in a whole other statement.
You need to just check your target date is within the desired range,
try
where Subscription_end_date >= dateadd(month,1,convert(date,getdate()))
and Subscription_end_date < dateadd(month,2,convert(date,getdate()))
Assuming of course you are running your report on the 1st of each month.
The first date of the next month can be found using:
dateadd(day, 1, eomonth(getdate()))
So, you can use this in a where clause:
where Subscription_end_date >= dateadd(day, 1, eomonth(getdate())) and
dateadd(day, 1, eomonth(getdate())) < dateadd(month, 1, dateadd(day, 1, eomonth(getdate())))

How to retrieve records that are from two months from the current date

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

How to pull week to date data in SQL Server?

I'm trying to pull week to date data for a recurring report that needs to go out daily. For eg: report that goes out Monday needs to have data for Monday, report that goes out Tuesday will have data for Monday and Tuesday etc. for the current week.
I know how to pull last X days data with :
my_date > DATEADD(day, -6, GETDATE())
How do I pull only week to date?
Use below SQL code in where condition:
where
my_date > DATEADD(dd, -((DATEPART(WEEKDAY, mydate) + 5) % 7), DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0))
The correct adjustment requires a bit of extra modular arithmetic and so the proper number of days to adjust backward works out to the expression below. I am also assuming that datefirst is set to Sunday:
-((DATEPART(WEEKDAY, mydate) + 5) % 7)
If you just subtract two from the day number given by datepart() then you'll end up with a negative number on Sundays which then cause dateadd() to jump forward. You can see a listing of dates and comparison here:
https://rextester.com/IUY88999

How to Target Last Month in SQL Query

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.

SQL server - Select all items with a date on the previous month

I have a table in my SQL Server database called "items" which has a column called "dateFinished".
I have a script that will run on the 1st day of each month which needs to select all items that finished in the previous month.
So, for example, on the 1st February it will need to select all items where the dateFinished is greater than or equal to 00:00 on the 1st of January and less than 00:00 on 1st February.
it also needs to work across new years (e.g. DEC - JAN).
Any ideas?
Select *
from items
where datefinished >= dateadd(m, datediff(m, 0, GETDATE()) - 1, 0)
AND datefinished < dateadd(m, datediff(m, 0, GETDATE()), 0)
You could get a day of the previous month with dateadd(m,-1,getdate()). Then, filter on the year and month of that date in a where clause, like:
select *
from items
where datepart(yy,dateFinished) = datepart(yy,dateadd(m,-1,getdate()))
and datepart(m,dateFinished) = datepart(m,dateadd(m,-1,getdate()))
This should work across years, and also if the query is run on a later day than the first of the month.
Simple just use what I just used: DATEDIFF(mm,dateFinished,GETDATE()) = 1
SELECT *
FROM items
WHERE DATEDIFF(mm,dateFinished,GETDATE()) = 1
I would start by checking out the DATEADD function
http://msdn.microsoft.com/en-us/library/ms186819.aspx