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

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.

Related

How to pass a date parameter in a SSRS report query string parameter?

I need to pass a date parameter as per below url. I'm trying this from the browser:
http://Server1/ReportServer_SQLTest?/Accounting%20Statements
&rs:Command=Render
&rdt=1115
&prt=187
&edr=11%2f02%2f2021TO
&cdr=11%2f02%2f2021TO
&sdr=11%2f02%2f2021TO
&ed=11%2f02%2f2021
&cd=11%2f02%2f2021
&sd=11%2f02%2f2021
&Grp=45
&ReportFormat=consolidated
&filter1=3
&event=187
&StyleOption=1
&vis_usr=-9999
&call=51fc7815-18e6-4574-a59a-c8a9d223df64
&rs:Format=pdf
it threw an error
Default value or value provided for the report parameter 'edr' is not a valid value. (rsInvalidReportParameter)
I tried in various ways passing the edr date parameter. Still throws error. Any suggestions?
It would look like the issue is that your edr, cdr and sdr parameters all have TO at the end of the passed value, which is not part of any valid date format that I am aware of.
I solved the problem. In SSRS TO means Today; and YS means Yesterday. The SSRS validates if today is 11%2f02%2f2021TO; So I changed as below. I took these parameter values from existing db entries.
&edr=11%2f09%2f2021TO
&cdr=11%2f09%2f2021TO
&sdr=11%2f09%2f2021TO

Argument data type nvarchar is invalid for argument 3 of convert function in SSRS

I am having an issue with a parameter and the convert function when executing my query in Report Builder.
I am having the following in my code:
CONVERT(VARCHAR(11), COALESCE(Status.POBDate, Status.[Sched Collection Date]),(#DateFormat)) AS [Collection Date]
,CONVERT(VARCHAR(11), Status.[Act Del Date],(#DateFormat)) AS [Delivery Date]
The (#DateFormat) parametner has data type Integer and available values as per the bild below.
The funny thing is that I can run the query in SSMS without any problem, but when trying to apply some adjustments in Report Builder, and save the report, it is complaining about the invalind argument even though, the parament (#DateFormat) was not edited anyhow. The report worked perfect online and only after opening it in Report Builder it started to complain also when I do not apply any new adjustments.
Any idea what can be wrong and how I could solve it?
I have checked some ideas here on stackoverflow, but nothing worked out so far.
Tones of thanks in advance!
Your parameter type is text not integer and that causes the error.
You can verify it by casting the DateFormat parameter to INTEGER in your SQL code
CONVERT(VARCHAR(11), COALESCE(Status.POBDate, Status.[Sched Collection Date]),CAST(#DateFormat AS INTEGER)) AS [Collection Date]
As a suggestion use the parameter to format the dates inside the report.
How to do it:
Remove conversion from your SQL code
In your parameter set it to text and as values set the date format string
For US ="dd/MM/yyyy"
For UK ="MM/dd/yyyy"
For your date fields set the format expression to = Parameters!DateFormat.Value

SSRS dataset filter

Hi I'm trying to build a report in ssrs which gives the output based on current weekending date where end day is saturday. I'm pulling my data from SSAS Cube and I have a weekending date field so I applied a filter in the query designer to get the current weekending date using a parameter I used this expression:
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today)
When I'm executing my query, it throws an error saying the restriction imposed by the constrained flag in the STRTOSET function were violated.
Please let me know how can I fix this.
The error is thrown because you are passing a string that doesn't correspond with a valid member in your cube.
Note the Query Designer builds a MDX query based on the dimensions and members you select, this query uses the STRTOSET() function to convert your string parameter in a valid member.
...
STRTOSET(#CurrentWeekendDate,CONSTRAINED)
...
If you pass the current weekend date to your parameter it produces:
STRTOSET('2016-01-10',CONSTRAINED)
As 2016-01-10 is not a valid member in your Date dimension it throws the error.
You have to pass something like this depending on your cube:
[Date].[Date Key].&[2005-01-01T00:00:00]
So in SSRS you have to set your parameter to Text and use this expression:
CORRECT EXPRESSION:
="[200 Date].[Week Ending Date].&[" &
Format(
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today),
"yyyy-MM-ddThh:mm:ss"
)
& "]"
UPDATE: If [Week Ending date] level doesn't include time:
="[Date].[Date Key].&[" &
Format(
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today),
"yyyy-MM-dd"
)
& "]"
Go through this tutorial if you get stuck.
Let me know if this helps.

what is correct format to pass Today as work item createdDate parameter

I have created data set (as TFS drill through report) using TFSOlap data source which is displaying all bugs based on created date. Now, I want to pass date range as parameter from main report so drill through report displays data for the bugs created between given date range. I am getting error "string.. can not be converted to date type" I am using following formatting expression at drillthrough report parameter:
="[Work Item].[System_CreatedDate].&[" + FORMAT(Parameters!FromWorkItemSystemCreatedDate, "yyyy-MM-dd") + "T00:00:00]"
Please let me know what type of expression should be used to avoid error. I tried with using CDate() instead of FORMAT but still getting problem.

Display TimeSpan correctly in SSRS

I have a TimeSpan value of 00:02:02.0 basically 0 hours 2 minutes 2 seconds and 2 milliseconds.
I will always have 0 hours. How do you format ssrs to read 2.02.0? I know you can do =Fields!FinishTime.Value.ToString("mm:ss:fff") however it always produces #error as value.
Thanks!
You are pretty close, ToString() is correct since SSRS doesn't know how to work with TimeSpans, but you'll need to wrap a Format around it to use the format string and convert it back to a date to use the date formatting patterns:
=Format(CDate(Fields!FinishTime.Value.ToString()), "mm:ss:fff")
EDIT:
If your dataset contains NULLs, you won't be able to use a simple IIF(IsNothing(Fields!FinishTime.Value), Nothing, <format formula>) because IIF does not short-circuit and you'll see an #error when it tries to use the ToString() function on NULLs. So instead try some custom code (retrieved from an MSDN forum post):
Function FormatTimeSpan(TS as TimeSpan) as String
Dim DT as new DateTime(TS.Ticks)
Return DT.ToString("mm:ss:fff")
End Function
And call it like this:
=IIF(IsNothing(Fields!FinishTime.Value), Nothing, Code.FormatTimeSpan(Fields!FinishTime.Value))
There is a much simpler way to do this, assuming you are using a SQL Server data source. In your SQL statement use this where you select the FinishTime field:
SELECT Convert(datetime, TableName.FinishTime) AS FinishTime, ...
It handles nulls just fine, and you can use the built-in formatting options in SSRS to format it.