At the point of running some code I want to be able to return the next 31st of March.
So if run today tye date returned would be 31st March 2023. But if the same code was run back on say the 1st of March 2022 then the 31st of March would be returned....not sure of the best way to achieve this.
A little date arithmetic should work here:
DATEFROMPARTS(YEAR(DATEADD(MONTH,-3,GETDATE())+1),3,31)
Hi thanks for getting me on the right track. I went with this very similar code -
DATEFROMPARTS (YEAR(DATEADD(MONTH,-3,GETDATE())+1), 3, 31 )
Related
I need to return the year (number) for the previous month. Like 15-Jan-2023, returns 2022.
Simple but I could not find any related solution.
So far I´m using -31 days but this is not as good as February I need to take care and adjust.
< year(getdate()-32)
SELECT DATEPART(YEAR,DATEADD(MONTH,-1,GETDATE()))
example:
SELECT DATEPART(YEAR,DATEADD(MONTH,-1,'15-JAN-2023'))
returns 2022
I need to return both the last instance of the 25th of the month before today and next instance of the 25th after today in SQL Server 2014.
I don't have code ready for this as I'm not SQL proficient
As an example
if Today is 28th June, I need to return 25th June and 25th July
If Today is 15th June, I need to return 25th May and 25th June
There wont be a case where this is requested on the 25th of each month, so no validation required there.
One idea using DATEFROMPARTS (assumes you're using 2012+, but with 2008 having 4 weeks of support left, a "safe" assumption no?):
SELECT DATEFROMPARTS(YEAR(V.Today),MONTH(V.Today),25),
DATEADD(MONTH,1,DATEFROMPARTS(YEAR(V.Today),MONTH(V.Today),25))
FROM (VALUES(DATEADD(DAY, -25,GETDATE()))) V(Today);
For today, that returns 2019-05-25 and 2019-06-25 and returns the correct values for your example: I.e.:
SELECT DATEFROMPARTS(YEAR(V.Today),MONTH(V.Today),25),
DATEADD(MONTH,1,DATEFROMPARTS(YEAR(V.Today),MONTH(V.Today),25))
FROM (VALUES(DATEADD(DAY, -25,'20190628'))) V(Today);
I hope someone can share their experience with me. I've used the following ssrs expression to default a SSRS report parameter to the last day of the current month for months without issue:
=DateAdd(“d”, -1, DateSerial(Year(Now()), Month(Now()), 1))
This morning instead of returnng 3/31/2016, the expression is stuck on 3/29/2016. Can anyone help me understand why this happened?
You are substracting one day to the first day of the current month.
You have to get the first day of the next month and substract one day.
=DateAdd("d", -1, DateSerial(2016, Month(Now.AddMonths(1)), 1))
It returns 3/31/2016
Let me know if this helps.
The expression you show will give the last day of the previous month, not the last day of this month.
You want to do date arithmetic rather than build dates from scratch. Think about what will happen in December if you are using the current year but adding a month to find the month you need.
The safest way to get the last day of the current month would be to calculate the first day of this month, add one month then subtract a day:
=DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Month, 1, DateAdd(DateInterval.Day, 1-Day(Today), Today)))
If what you actually want is the last day of the previous month, then that is simple:
=DateAdd(DateInterval.Day, 0-Day(Today), Today)
Disclaimer: I am just looking for a logic not code
John discovered a strange island called Rasa. The years and weeks on the island are weird. Digging deeper into the island's calendar, he found out that it is similar to rest-of-the-world's (ROW) calendar but the island calendar's Year starts on 1st week of February's calendar. John is asking you to help him solve the problem of converting ROW's calendar into Island's calendar. Here is the question.
You are given a date (today's date). You have to determine the Island week's number. The catch here is that the Island year starts from 1st week of February and every Island's week starts from Sunday and ends on a Saturday. Write a SQL statement in SQL Server to achieve this. Use SQL Server functions and devise a logic.
Input parameter: Any date.
Output parameter: Week No in Rasa Calendar.
Here is an example:
Date: 5th May 2015 --
Week No in ROW Calendar:19
Week No in Rasa's Calendar:14
Date: Jan 1 2017:
Week No in ROW Calendar:1
Week No in Rasa's Calendar:49
My question: can this be achieved in SQL Server?
My homework: I tried a couple of ways to solve the problem.
Approach #1:
Step 1: Calculate the total no of days between today and Feb 1.
Step 2: Divide it by 7 and add 1 to the result.
Later found out that this approach will not work if Feb 1 is on any day other than Sunday.
Eg: 1st Feb is on Wednesday. 5th Feb will be on Monday
So, 1st Feb is on Rasa's week 1, and 5th Feb is on Rasa's week no 2. According to my approach 1st and 5th feb are on week 1 which is incorrect.
Approach #2:
I thought removing 5 weeks of Jan from ROW's calendar should work
select
case
when f.RasaWeek = -4 then 48
when f.RasaWeek = -3 then 49
when f.RasaWeek = -2 then 50
when f.RasaWeek = -1 then 51
when f.RasaWeek = 0 then 52
else
f.RasaWeek
end as Rasa_week,
f.year, f.month, f.date
from
(select
datepart(wk, date) - 5 as RasaWeek, *
from
<datetable>
where
Year(date) in (2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018)) as f
Info: I tested this on a <datetable> but this code will break if there is a 53rd week. Notice that I was not able to take care of the 53rd week.
Any inputs to solve this problem are welcome.
With dates, it's almost always easier to make a calendar table and store the data you care about rather than trying to do anything beyond basic date arithmetic. Use SQL's strengths: storing and retrieving data.
In this case, what you care about are all of the first Sundays in February. If you store these dates in a table, the solution is:
RETURN
SELECT TOP 1
DATEDIFF(day,[date],#input_date) / 7
FROM IslandCalendarStartDates
WHERE [date] <= #input
ORDER BY [date] DESC
This way you don't need to worry about leap years or 53-weeks, or any of the edge cases. Just count the days from the most recent first Sunday in February and divide by 7. If you need to change the solution to accommodate a different start date, you only change the data, not the code.
I have an ssrs matrix with total in rows and months as columns .. something like this .
Jan Feb March
Total1
Total2
I can choose a month as a parameter and my matrix shows the results for the month and the two months before the current month.
When I choose August :
I can see June July August
When I choose September :
I can see July August September
But when I choose October
I see October August September
My client wants to see it as August September October
I don't know the reason for this behavior .But can anyone help me with this ??
I tried doing the sort for the tablix as follows :
=(Fields!Month.Value)
Month is a number like 1 for Jan, 2 for Feb. I add it as a sorting expression but I dont see any change in the sorting.
according to your data.
August = "8"
September "9"
October = "10"
it looks to me like you need to convert your month to an integer type, Sort is picking up your month as a string value.
try converting your month field to an integer in your sort expression and then running rerunning the report.
something like:
=CInt(Fields!Month.Value)
I finally found what was going wrong with my solution .
I was adding the sorting on the "tablix properties" section .
Sorting needs to be added on the "Column group" and not on the "tablix properties" and this did the trick in my case. :-)