SSRS 2008 Time Conversion within IIF Statement - reporting-services

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.

Related

How to get around '#Error' when a function in MS Report Builder is evaluating a null value?

I hope my title is clear enough. I'm working in MS Report Builder, using a function that applies a regular expression to a queried value in order to get back a certain substring. The regex works fine, so I'll demonstrate a simpler version here to make this less wordy. Here's the gist of my equation:
=IIF(Len(Fields!CourtLocation.Value) < 1, "none",System.Text.RegularExpressions.Regex.Match(Fields!CourtLocation.Value, "(?:[A-Z]{2,4})").Value))
The main purpose is to get that substring, but I added the IIF so that on those occasions when the CourtLocation.Value is empty (I tried Is Nothing in my expression as well), the function returns "none" rather than "#Error"
I've been looking around for a solution, but nothing has worked; it seems like most other people who talk about this are using a mathematical equation rather than trying to get a string. Can anyone help me get rid of that stupid "#Error" value?
You could try this (untested)
=System.Text.RegularExpressions.Regex.Match(
IIF(
Len(Fields!CourtLocation.Value) < 1,
"none",
Fields!CourtLocation.Value
)
, "(?:[A-Z]{2,4})"
).Value
This way the IIF is performed on the string that you want to pass to the regex function, so it always gets a valid value to process
Iif evaluates both sides, so you can nest two Iif statements to avoid the error.
Did you already read this one?
https://sqldusty.com/2011/08/01/ssrs-expression-iif-statement-divide-by-zero-error/
I'll copy the text into the answer if that solves it for you.

SSRS Report Field with a Dynamic Data Type

I'm amalgamating several manual reports into one SSRS one by changing the select statements to follow a set format of:
select
'DepartmentName'
,'StatName'
,'DataType' --(Int, percentage,date,time, etc)
,'Stat'
from TablesNeeded
Now this unions together fine so I end up with a list of statistic names and their values. After this I want to have the cell showing "Stat" to change it's formatting based on the result of "DataType"
I attempted to use an IIF to determine the format, which behaved with a single IIF statement, however after nesting to accomodate different data taypes it appears to "false" every IIF result and return just the default/value:
Working
=iif(
Fields!DataType.Value="Percentage"
,Format(Fields!Stat.Value,"0%")
,Fields!Stat.Value
)
"Falsing"
=iif(
Fields!DataType.Value="Percentage"
,Format(Fields!Stat.Value,"0%")
,iif(
Fields!DataType.Value="Date"
,Format(Fields!Stat.Value,"y")
,iif(
Fields!DataType.Value="int"
,Fields!Stat.Value
,Fields!Stat.Value
)))
and using switch similarily also "Falsed"
=Switch(
Fields!DataType.Value="Percentage",Format(Fields!Stat.Value,"0%")
,Fields!DataType.Value="int",Format(Fields!Stat.Value,"#,#0")
,Fields!DataType.Value="Date",Format(Fields!Stat.Value,"y")
)
I've asked a colleague and we're both stumped. Any ideas on proper expression formats?
Assuming all your datatypes are correct then you just need to use a simplified version of your switch statement in the Format property of the cell(s).
Something like..
=SWITCH(
Fields!DataType.Value="Percentage","0%"
,Fields!DataType.Value="int","#,#0"
,Fields!DataType.Value="Date","y")
)
Note You could just use "p0" for you percentage type, "n0" for your int.

How to implement Having Clause in SSRS

I'm New to SSRS.
Im trying to create a report where i need to group by [DATA Flag] column which is working fine ,but once the data is grouped i need to set the DATA FLAG ="TotalCancellations" and there is another column CancellDays which i need to set it as <120 .
I tried
Option 1:-
So to achieve this i have added TWO filters one with
Expression : DATA FLAG
Operator =
and Value as TotalCancellations
and the other filter as follows
Expression : Cancelldays
Operator =
and Value as < 120
But its not working and giving empty result,i have records with Cancelldays <120
Option 2 :-
Right click on Group and in General Tab ,Group on Expression as below ]
Fields!DFlag.Value = "TotalCancellations" AND Fields!DFlag.Value <120
which didnt work :(
this is similar to writing having clause in SQL i believe but im not getting how to implement that here in SSRS.
i can add in SQL Query but its already a huge query with lot of unions so please suggest me if there is any way i can implement here in SSRS
Im using Matrix in SSRS 2008
Adjusting the syntax for your option 1 is probably the easiest solution. In the Group Properties, under the Filters section, enter these two filters:
Expression: [DFlag] (Text)
Operator: =
Value = TotalCancellations
Expression: =Sum(Fields!CancelDays.Value) [enter this in the expression builder] (Integer)
Operator: <
Value: 120
Putting all the filters in a single expression, like your option 2, can be useful if you need to filter by one criteria OR another.
Below are the filer expressions
and this is how group by is
The filter should be implemented at the tablix level, not at the group level.

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.

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))