Monday, April 9, 2012

SQL Report Service with Visual Studio 2008 - date (my cheat sheet)

There are many ways to calculate dates.
Here are some useful date calculations.
Thank you all who created and posted each one of these on their websites or blogs.

--Monday (current week):
=DateAdd("d", -(WeekDay(Today(),2))+1, Today())

--Next Monday:
=DateAdd("d", 7-(WeekDay(Today(),2))+1, Today())

--First day of month:
=DateAdd("d",1-DatePart("d",Today()),Today())
--First day of next month:
=dateadd("m",1,DateAdd("d",1-DatePart("d",Today()),Today()))
--First day of year:
=DateSerial(YEAR(Today()),1,1)

So based on these expressions we can change Today()'s value and numbers to calculate other days.
For instance,
--using a parameter value
=DateAdd("d", -(WeekDay(Parameters!Parameter1.Value,2))+1, Parameters!Parameter1.Value)

--First day of next year:
=DateSerial(YEAR(Today())+1,1,1)

--Last Day of previous week(Sunday)
=DateAdd("d", 1-Weekday(today),Today)

and so on....
 

No comments:

Post a Comment