Display TimeSpan correctly in SSRS - reporting-services

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.

Related

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 2008 Time Conversion within IIF Statement

I am trying to convert a time within an IIF statement.
I have a field with a value of: 2013-10-15 01:00:00.0000000. I am trying to convert that to "01:00" and then produce 1 of 2 results based on the conversion.
I have tried both using a case when statement which does not seem to work.
I have also tried using like "*01:00*" which also does not work. Currently, this is what I'm attempting to do, but it seem that I'm not going about this the right way:
=iif(Fields!TEST1.VALUE LIKE ("*01:00:00.0000000*"), "TRUE", "NOT TRUE")
Looking strictly at what you're trying to do, with an SSRS expression you need to use InStr to check for a substring in a larger string:
=IIf(InStr(Fields!TEST1.Value, "01:00:") > 0, True, False)
This works fine on your example row and another test case:
However, we know very little about your data - is the underlying data actually a string or a datetime?
If your data is datetime, you can use a different approach:
=IIf(Hour(Fields!TEST1.Value) = 1 and Minute(Fields!TEST1.Value) = 0
, True
, False)
Or even:
=IIf(Fields!TEST1.Value.ToString("HH:mm") = "01:00"
, True
, False)
If possible, I would consider datetime functions if at all possible as they are closest to the intent of what you seem to be trying to achieve.

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.

SSRS error: Conversion from string "string;#NA" to type 'Date' is not valid

My datasource is XML (sharepoint list). I basically want to show the date formatted if the value in the field is a date and if not then show "NA". For some reason, even when the data is a a string it is still trying to convert it to a date somewhere. Here is my code..
=IIF
(
ISDATE(replace(First(Fields!ows_Manufacturing_Date.Value, "DataSet1"),"datetime;#","")),
formatdatetime(replace(First(Fields!ows_Manufacturing_Date.Value, "DataSet1"),"datetime;#",""),2),
replace(First(Fields!ows_Manufacturing_Date.Value, "DataSet1"),"string;#","")
)
The problem is that the IIF statement in SSRS doesn't short circuit, it always evaluates both conditions, so even if the field is not a date, it still tries to do the formatdatetime function.
(See this: SSRS iif function question)
Instead of the IIF function, try using the SWITCH function instead:
=SWITCH(First(Fields!ows_Manufacturing_Date.Value, "DataSet1")="string;#NA",
"NA",
First(Fields!ows_Manufacturing_Date.Value, "DataSet1")<>"string;#NA",
formatdatetime(replace(First(Fields!ows_Manufacturing_Date.Value, "DataSet1"),"datetime;#",""),2))

Code not running when Field is NULL - SSRS 2005

I have a textbox in my SSRS 2005 report. The expresssion for this textbox is:
=IIF(IsDBNull(Fields!fOrgID), Code.SetMyVar("null"), Code.SetMyVar(Fields!fOrgID.Value))
I have also tried IsNothing(Fields!fOrgID) and a few other variations of checking for nulls.
I have modified the SetMyVar function for testing and it now looks like this:
Public Function SetMyVar (var as String)
MsgBox(var, VbOKCancel, "Test1")
If var Is Nothing Then
Return "NOTHING"
Else
MyVar = var
Return var
End If
End Function
I also have the public variable MyVar:
Public Shared Dim MyVar as String
When my database query returns data, this correctly evaluates, a messagebox is displayed with the value, the textbox gets set with the value, and the world is generally a happier place.
When my database query does not return a value though, I get the error:
The query returned no rows for the data set. The expression therefore
evaluates to null.
and the SetMyVar function never appears to be ran (you never get the messagebox popup). As expected, my emotions range from anger, sadness, and bitter hatred of SSRS.
I read something about SSRS evaluating both sides of an IF statement, so perhaps that is why I get the error (likely then on Code.SetMyVar(Fields!fOrgID.Value))... not sure how I get around that though.
Thoughts? Suggestions? Words of comfort?
From the sound of things, it seems likely that the issue is that SSRS is having a problem displaying zero records. I'd recommend one of the following:
1) Use a control that handles zero records appropriately (Tables do. I think Lists do as well).
2) Modify your query to return a single record with blank values if it would otherwise return zero records.
An answer to the original question:
=IIF(IsNothing(Fields!fOrgID),
Code.SetMyVar("null"),
Code.SetMyVar(IIF(IsNothing(Fields!fOrgID),"Foo",Fields!fOrgID.Value)))
The error was from both sides of IIF being evaluated. The extra IIF in the statement above will avoid Code.SetMyVar from ever being called with a null value.
I believe you're right about about Iif always evaluating both of its value arguments (at least, it does in Visual Basic). I'm not sure why you're getting this precise error (unless strings can't be assigned a value of DBNull?), but you almost certainly want to attack this problem with a different method.
The reason for this is that your current code will likely always call both set methods regardless of the conditional value.
Formula that worked for my SSRS 2008 reports.
=IIf(String.IsNullOrEmpty(Fields!NullableFieldwithPossibleBlankStrings.Value),"Yes","No")
I tried this too (also tried a version with IsNothing)...
=Code.SetField(IsDBNull(Fields!fOrgID))
And changed the function to be one that accepts a boolean. I figure this above function would always return a true or false, but in the event of a NULL, I again get "The query returned no rows for the data set. The expression therefore evaluates to null.".
I need to pass back to my code if the field is null or not (as this will let me know if the datasource is null or not).
Let me know if you can think of a better way because I cannot.