SSRS DateDiff having errors due to syntax - reporting-services

I tried a few ways of doing this but keeps erroring out. Have to convert a Crystal report to SSRS. Suggestions?
Crystal
DateDiff ("d",{CRSS_INSP_DET.NEXT_INSP_DTE},CurrentDate())
SSRS - both of these do not run
DateDiff("day", Fields!INSP_DTE.Value), Today())
DateDiff("day", Fields!INSP_DTE.Value), Now())
It says this
The Value expression for the textrun
'WRK_AAR7.Paragraphs[0].TextRuns[0]' contains an error: [BC30516]
Overload resolution failed because no accessible 'DateDiff' accepts
this number of arguments.

You have an extra ) in your expression.
DateDiff("day", Fields!INSP_DTE.Value, Today())
DateDiff("day", Fields!INSP_DTE.Value, Now())

Related

DateDiff function in SSRS (report server) gives error?

Im trying to find the days gap between two dates using DateDiff function.
I have 2 datasets defined. If companycode is 'AB' then from one dataset else from another dataset I retrieve data.
Here is my expression. When I change to preview mode, it shows redmark to the first First(Fields!PeriodFrom.Value line. Why? (after generating report that field shows #Error
What Im doing wrong here?
=IIF(Parameters!CompanyCode.Value="AB",
DateDiff("d",First(Fields!PeriodFrom.Value, "ABReportData"), First(Fields!PeriodTo.Value, "ABReportData")),
DateDiff("d",First(Fields!PeriodFrom.Value, "XYReportData"), First(Fields!PeriodTo.Value, "XYReportData")))
I think there are two possible scenarios. First one is the expression
=First(Fields!PeriodFrom.Value, "ABReportData")
doesnt return a value. Add a column with this expression and check if you get a value.
If the value is correct, make sure that the DateDiff() function gets a date:
=IIF(Parameters!CompanyCode.Value="AB",
DateDiff("d",
CDate(First(Fields!PeriodFrom.Value, "ABReportData")),
CDate(First(Fields!PeriodTo.Value, "ABReportData"))
),
DateDiff("d",
CDate(First(Fields!PeriodFrom.Value, "XYReportData")),
CDate(First(Fields!PeriodTo.Value, "XYReportData"))
)
)

Error when trying to DATEADD milliseconds in SSRS

So I have an enddate parameter in my report and I have the following code under Default Value to get me the last day of the previous month.
=DateAdd("d", -1, DateSerial(Year(Now()), Month(Now()), 1))
This works fine, returning 04-30-2016. That's fine and dandy but when I go to use the Subscription piece in SSRS it has the datetime field as: 04-30-2016 12:00:00 AM. So I tried doing the following to my code to have it shave off three milliseconds:
=DateAdd("ms", -3,DateAdd("d", 0, DateSerial(Year(Now()), Month(Now()), 1)))
When I try to run my report I get the following error:
The DefaultValue expression for the report parameter "enddate" contains an error: Argument 'Interval' is not a valid value. (rsRuntimeErrorInExpression)
How do I fix this error and get my report running as it should?
The AddMilliseconds function will work for you in this case.
=DateAdd("d",-1,DateSerial(Now.Year,Now.Month, 1)).AddMilliseconds(-3)
This gives you the last day of the previous month substracting three milliseconds.
Let me know if this helps.

Comparing todays date against first day of the week

I am trying to set a default parameter value in SSRS report. I want to test the current date to see if it equals the first day of the week (in my case Monday). If it is the first day of week, then I want the default value to be current date minus 2 days, if it is not the first day of the week then I want the default value to be current date minus 1 day.
I seem to have a syntax problem but it doesn't tell me where. My parameters are StartDate and EndDate.
this is what I've tried:
=iif(weekday(Today(),FirstDayOfWeek.Monday)==1,DateAdd("d",-2,today(),DateAdd("d",-1,today())
this is the generic error I get:
The value expression for the report parameter 'StartDate' contains eror:[BC30201] Expression expected.
Where am I going wrong?
You are trying to use Sql syntax in a SSRS VBA expression. SSRS VBA allows very similar expressions for date manipulation to Sql, the main difference being the use of the DateInterval enum.
So your expression needs to use VBA syntax:
=IIF(Weekday(Today, FirstDayOfWeek.Monday) = 1, DateAdd(DateInterval.Day, -2, Today), DateAdd(DateInterval.Day, , -1, Today))
It appears that you are missing a closing parentheses after the first logical part of the if statement and another to close the statement.
=iif(weekday(Today(),FirstDayOfWeek.Monday)==1,DateAdd("d",-2,today()),DateAdd("d",-1,today()))

Reporting services expression for default value

the following error is displayed when deploy reports,
"The Value expression for the report parameter ‘Year’ contains an error: [BC30201] Expression expected "
The problem is in default value expression for parameter Year.
=Switch(
CInt(System.DateTime.Now.Month) >3,
Year(Today()),
CInt(System.DateTime.Now.Month) <=3,
{Year(Today.AddYears(-1)),Year(Today())}
)
Any idea?
I'm pretty sure you can just rewrite the above as:
=IIf(Month(Today) > 3, Year(Today), Year(DateAdd(DateInterval.Year, -1, Today)))
i.e. If Today is in April to December, use the current year, else use the previous year.

How to convert separate date and time parameters to a valid datetime?

I have a report with date and time (only an hour and minutes) parameters which are separate. The Date parameter is of type Date/Time, and the Time parameter is set as Text. I want to merge those two values into one because I want to pass a minimum number of arguments to the stored procedure. I tried to achieve that goal in many ways but SSRS returns an error for every attempt.
If I try to use expression like this:
=Format(FormatDateTime(Parameters!startDate.Value, DateFormat.ShortDate).ToString() + Parameters!startTime.Value, "dd/MM/yyyy HH:mm")
SSRS returns this error:
Error conterting data type nvarchar to datetime.
And when I tried to use Datetime.Parse like this:
=DateTime.Parse(Format(FormatDateTime(Parameters!startDate.Value, DateFormat.ShortDate).ToString() + Parameters!startTime.Value, "dd/MM/yyyy HH:mm"))
SSRS said:
The Value expression for the query parameter '#startDate' contains an error: The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
When I removed the FormatDateTime function I get yet another error:
The Value expression for the query parameter '#startDate' contains an error: Input string was not in a correct format.
Do you have any ideas how to write this expression correctly?
PS. I use SSRS 2008 R2.
This works for me:
=CDate(Format(Parameters!Date.Value, "yyyy-MM-dd") + " " + Parameters!Time.Value)
Didn't try and troubleshoot your specific examples, but they may be running into issues where you're not including the space between the date and time.
The above expression may be suitable for your report; don't know robust it would be in different locales.
You could also consider doing the concatenation/conversion in custom code if you need more flexibility.