SSRS tweak to time expression - reporting-services

I am currently using this expression to show the previous working day in an SSRS report:
=DateAdd("d"
, Switch(DatePart("w", Today) = 2, -3
,DatePart("w", Today) = 1, -2
,True, -1)
, Today)
which works fine.
However I would like the output to be,if I ran the query today for example,:
24/04/2020 23:59:59
Instead of the current 24/04/2020
Please can you advise on how I could add hours, minutes and seconds- 23:59:59 - to the above expression?
Thank you

There may be a more elegant way of doing this but I based this on you current expression. I've text it and it seems to work OK.
=DateAdd("s"
, Switch(
DatePart("w", Today) = 2, (-3 * 86400) -1,
DatePart("w", Today) = 1, (-2 * 86400) -1,
True, -85401
)
, Today)
This simply does a datediff in seconds rather than days and then adjusts the amount of seconds to remove by 1

Related

Is there a way to write and DateAdd and DatePart SSIS Expression to always generate ThisWeekMonday's date using Getdate()?

I have a package that needs to be run every Monday, because the file dates all have Monday's dates for each week that it is sent. If the package fails for some reason, I want to I to write it in such a way that anybody else can rerun it on any other day of the week. Regardless of which day it is run, it must generate Monday's date of each week to pick up the correct file.
I tried using the following expression, but it generated last week's Monday's date when it is run on a Monday:
DATEADD( "dd", -1 - (DATEPART("dw", GETDATE()) + 4) % 7, GETDATE() ).
I altered the parameters several times, but still couldn't get it to work.
Someone gave me this expression, but it didn't work at all for Monday even after changing the parameters:
DATEADD( "dd", (DATEPART( "dw", GETDATE() ) -3), GETDATE() )
Finally, I tried using the following statement which is equivalent to a Case statement in SQL, but it gave me an error in the first part of each line shown below (NOTE: square brackets were not included.)
[ DATEPART( "dw", GETDATE()) ]
See my complete expression below:
DATEPART( "dw", GETDATE()) == 1 ? DATEADD( "dd", 1, GETDATE()) : (
DATEPART( "dw", GETDATE()) == 2 ? DATEADD( "dd", 0, GETDATE()) : (
DATEPART( "dw", GETDATE()) == 3 ? DATEADD( "dd", -1, GETDATE()) : (
DATEPART( "dw", GETDATE()) == 4 ? DATEADD( "dd", -2, GETDATE()) : (
DATEPART( "dw", GETDATE()) == 5 ? DATEADD( "dd", -3, GETDATE()) : (
DATEPART( "dw", GETDATE()) == 6 ? DATEADD( "dd", -4, GETDATE()) : (
DATEPART( "dw", GETDATE()) == 7 ? DATEADD( "dd", -5, GETDATE()) ))))))
Can anyone help me to resolve this?
Thanks in advance.
Use the following expression:
DateAdd("dd", 2 - DatePart("dw", GetDate()) , GetDate())
To explain, 2 (which is Monday) - any other day of the week gives you the day offset which can be added to the current day.
e.g. 2 (Monday) - 6 (Friday) = -4 so adding -4 days to Friday gives you Monday.
Hope this helps.

Output First of Month Following 60 Days From a Stored Date in an Access qry

I have a date field in access where I have to calculate the first of the month following 60 days from that date. For example my date is 12/12/2017. I need to output 3/1/2018, First of the month following 60 days from 12/12/2017. I know how to use the DateAdd function to get my 60 days, but am confused as to how to output the first of the month following those 60 days.
Thanks,
Mark
There are multiple ways to do this. One way using DATEADD():
SELECT DATEADD("d", -"d"(DATEADD(MONTH, 1, DATEADD("d", 60, '2017/12/12')))+1,
DATEADD("m", 1, DATEADD("d", 60, '2017/12/12')))
If you have Access 2013+ you can use EOMONTH()
SELECT DATEADD("d", 1, EOMONTH(DATEADD("d", 60, '2017/12/12')))
Use this expression:
DateThreeFirst: DateAdd("m", 3, DateSerial(Year([YourDateField]), Month([YourDateField]), 1))
To first deduct one day:
DateThreeFirst: DateAdd("m", 3, DateSerial(Year(DateAdd("d", -1, [YourDateField])), Month(DateAdd("d", -1, [YourDateField])), 1))
To use days' count and not months:
DateThreeFirst: DateSerial(Year(DateAdd("d", 60, [YourDateField])), Month(DateAdd("d", 60, [YourDateField])) + 1, 1)

Datediff in MsAccess

I am stuck in one place.
I am using DateDiff in Ms Access it is giving me proper output, like
StartDate is 10-Sep-2016
EndDate is 15-Oct-2016
Total Days which I will get is 35
& months will i get is 1 Month
DateDiff('d',StartDate,EndDate)
**But I want output as 2 months if it is exeeded the 30 days.
if it is 61 days then 3 months & so on.
**IIFFF days diffrence is
29 Days then output should be 1 months
30 Days then output should be 1 months
32 Days then output should be 2 months
60 Days then output should be 2 months
62 Days then output should be 3 months**
Will that be possible in the DateDiff in MsAccess
or is there any other function available so that i can achieve the same output.**
You can do this using conditional logic. Perhaps something like this:
select iif(DateDiff('d', StartDate, EndDate) > 30,
DateDiff('d',StartDate,EndDate) & " days",
"2 months"
)
Your logic that anything exceeding 30 days is "2 months" seems strange. Normally, I think the logic would look like this:
select iif(DateDiff('d', StartDate, EndDate) > 30,
DateDiff('d', StartDate, EndDate) & " days",
DateDiff('m', StartDate, EndDate) & " months"
)
will this logic suffice to modify your SQL function?
Public Function FN_GET_MONTH(iDays As Long, Optional iDaysInMonth As Long = 30)
If (iDays / iDaysInMonth) > iDays \ iDaysInMonth Then
FN_GET_MONTH = (iDays \ iDaysInMonth) + 1
Else
FN_GET_MONTH = (iDays \ iDaysInMonth)
End If
End Function
?FN_GET_MONTH(29) = 1
?FN_GET_MONTH(31) = 2
?FN_GET_MONTH(60) = 2
?FN_GET_MONTH(80) = 3
?FN_GET_MONTH(91) = 4
you can have this public function and use it in your SQL code like
FN_GET_MONTH(DateDiff("d", StartDate, EndDate))
This query seems to give the results you seek:
SELECT
StartDate,
EndDate
numDays,
((numDays - 1) \ 30) + 1 AS numMonths
FROM
(
SELECT
StartDate,
EndDate,
DateDiff("d", StartDate, EndDate) AS numDays
FROM YourTable
)
It gives me
numDays numMonths
------- ---------
...
29 1
30 1
31 2
32 2
...
59 2
60 2
61 3
62 3
...
It seems like your minimum count of months for a positive count of days is 1, thus:
MonthCount = Sgn(DateDiff("d",StartDate,EndDate)) + DateDiff("m",StartDate,EndDate)
Edit
For a 30-day cut that will produce your example output, use this simple formula in your query:
MonthCount: (DateDiff("d",[StartDate],[EndDate])-1)\30+1

SSRS Expression First Day of of First Week of Current Year

Everyone,
I have a question that has stumped me for a day and can't figure out. What I am looking for is a formula in SSRS Expression that will tell me what the date is for the first day of the first ISO week of the current year.
For Example:
2014 would yield: 12/30/2013. The reason for this would be that the first ISO week of the 2014 year is from (12/30/2013) - (01/05/2014).
2013 would yield: 12/31/2012
I would appreciate any help anyone?
Thanks,
You can use this function:
Public Function dtFirstDayOfISOYear(ByVal intYear As Integer) as Datetime
'the first week of a ISO year is the week that contains the first Thursday of the year (and, hence, 4 January)
Dim intDayOfWeek As Integer = CInt(New DateTime(intYear, 1, 4).DayOfWeek)
'ISO weeks start with Monday
If intDayOfWeek < DayOfWeek.Monday Then intDayOfWeek = intDayOfWeek + 7
Return DateAdd(DateInterval.Day, -intDayOfWeek + 1, New DateTime(intYear, 1, 4))
End Function
And call it using an Expression like this:
=Code.dtFirstDayOfISOYear(2014)
You can also use a standalone Expression like this:
=DateAdd("d", (-1) * (CInt(New DateTime(2014, 1, 4).DayOfWeek) + IIf(CInt(New DateTime(2014, 1, 4).DayOfWeek) < DayOfWeek.Monday, 7, 0)) + 1, New DateTime(2014, 1, 4))

Linq to SQL - Start Of month

I have an sql statement which returns me the 1st day of the month:
DATEADD(month, DATEDIFF(month, 0, #input), 0)
How would I recreate this call using SQLFunctions.DateAdd and SQLFunctions.DateDiff?
Thanks in advance
If you are really using LINQ-to-SQL:
var zero = new DateTime(2000, 1, 1);
var resxxxx = (from p in ctx.Addresses
select new {
month = zero.AddMonths(SqlMethods.DateDiffMonth(zero, p.Date))
}
).FirstOrDefault();
The important part is month = zero.AddMonths(SqlMethods.DateDiffMonth(zero, p.Date)).
Note that I've used the zero = new DateTime(2000, 1, 1);, but any 1st of the month would be ok (1st jan 1980, 1st jul 1983, 1st dec 2100...). It's the two 0s in your SQL command. p.Date is the date of which you want to calculate the first of the month (so #input).