Converting strings to DateTime in an IIF expression - reporting-services

I am using SSRS 2008R2 to create reports. The datasource is xml response from webservice. I want to return empty string if there is no data, but if there is row with data I want to convert it to another datetime format.
I am using IIF construction like this:
=IIF
(
LEN(Fields!DateOfReg.Value) <= 0,
"",
FORMAT(CDATE(DateTime.ParseExact(Fields!DateOfReg.Value,"M/d/yyyy hh:mm:ss tt",
System.Globalization.CultureInfo.InvariantCulture)), "dd.MM.yyyy HH:mm:ss")
)
When there is no data it shows #Error in the textbox and in the output:
String was not recognized as a valid DateTime
Does it mean that the IIF construction calculates both TRUE and FALSE statements? If so how can I make it short-circuit and don't convert row if there is no data?

Try the following:
=IIF
(
Fields!DateOfReg.Value= "",
"",
Datevalue(Cstr(Fields!DateOfReg.Value))
)

Yes, you are correct - IIF calculate both statements. But you can convert your empty string to "Nothing" using IIF and then try format it, something like this:
Format(IIF(Fields!DateOfReg.Value = "", Nothing, Fields!DateOfReg.Value), "yyyy/MM/dd")
Format function cause exception if you pass empty string but return empty string if you pass "Nothing".

Related

Index was outside the bounds of the array in SSRS

I have two parameters , let's say P1 and P2. The sample expression I used for P2 is
IIF(P1.Label="string", "null" ,Split(P1.Label," ").GetValue(0))
When the condition is false, the split expression is working fine. But if the condition is true, I'm getting 'Index was outside the bounds of the array' error. If the condition is true, I need to pass the value "null" as varchar type.
Can someone please advice on this?
The problem with the IIF function is that it is a function not a language construct. This means that it evaluates both parameters before passing the parameters to the function. Consequently, if you are trying to do a Split on a parameter that can't be split, you will still get the 'Index was outside the bounds of the array' error, even when it looks like that code shouldn't be executed due to boolean condition of the IIF statement.
The best way to solve this is to create a safe string splitter function in custom code where you can use real language constructs. Also, check that the string is splittable by checking it contains a space instead of checking for a special string:
Public Function SafeSplit(ByVal SplitThis As String) As String
If InStr(SplitThis, " ") Then
Return Split(SplitThis, " ")(0)
End If
Return "null"
End Function
and then use this in your report for the Value expression instead of IIF:
=Code.SafeSplit(Parameters!P1.Label)

SSRS - Formula inside IIF statement

Im trying to create an IIF statement on SSRS where one of the results have an expression inside. I could only find examples with plain text but wanted to no how i can mix text and this expression as a result:
This is an example of what I'm trying to do:
=IIf(Sum(Fields!BytesTotal.Value, "dataset_traffic") = 0,
"No data available",
"Your data is 'Sum(Fields!BytesTotal.Value, "dataset_traffic")/1073741824'"
)
How can i declare an expression inside an IIF result?
Try this:
=IIf(
Sum(Fields!BytesTotal.Value, "dataset_traffic") = 0,
"No data available",
"Your data is " & Sum(Fields!BytesTotal.Value, "dataset_traffic")/1073741824
)
Let me know if this helps.

DB Date insert works sometimes

I have the following line:
command.Parameters.Add("#date", SqlDbType.DateTime, 8).Value = date_of_trip;
where date_of_trip is a string containing 25-9-2013 (i.e. day-month-year)
This gives me an error message:
System.ArgumentException: Cannot convert 25-9-2013 to System.DateTime.
Parameter name: type ---> System.FormatException: String was not
recognized as a valid DateTime.
However, if date_of_trip is 1-1-2013, it seems to work perfectly!
You should parse the string to datetime before you send it to the db.
For example:
DateTime dt;
if(DateTime.TryParse(date_of_trip, out dt))
{
command.Parameters.Add("#date", SqlDbType.DateTime, 8).Value = dt;
// ...
}
else
{
// you should use a CompareValidator to check if it's a valid date
}
If you want to ensure that your given format will work even if the current-culture of the server will change you can use DateTime.TryParseExact with format "dd-MM-yyyy":
if (DateTime.TryParseExact(date_of_trip, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt))
{
// ...
}
I guess it's because the server expects the first parameter to be a month. Try to use a real DateTime or the format yyyy-mm-dd (i.e. 2013-09-25), which should always work.

SQL Reporting 2008; Check if an Array Contains a String

In SQL Reporting 2008 how can I determine if an Array Contains a String?
Example, I wish the following to return "1":
IIf(Split("a,b,c", ",").CONTAINS("a"), "1", "0")
What may be used in replace of the above CONTAINS function? Is it impossible? This value'd be the FilterExpression for my table. Its purpose is to decide what to show and what to hide.
If you are looking for an answer only in an expression, I am not positive. However, you can write .Net methods and call them just like expressions from a custom dll or a "code" section of the report. If you use built-in code, you can do something like the following:
http://www.vbforums.com/showthread.php?t=558440
Creating inline code or referencing an assembly in SSRS:
http://bryantlikes.com/pages/824.aspx
UPDATE:
Example to get your delimited values from your concatenated string:
http://www.dotnetperls.com/split-vbnet
UPDATE:
Here is a function you can use. You put it in the code section of the report:
Public Function Contains(ByVal ItemToCheck As String, ByVal CommaValuesList As String, ByVal delimeter As Char) As Boolean
Dim commaValues() As String = Split(CommaValuesList, delimeter, -1, CompareMethod.Text)
For Each commavalue As String In commaValues
If ItemToCheck.ToLower.Trim = commavalue.ToLower.Trim Then
Return True
End If
Next
Return False
End Function
Use the following syntax to reference it:
=code.Contains(param1,param2,param3)
Let's use MyLettersParameter as a multiselect parameter. To determine if it contains "a" use:
=Array.IndexOf(Parameters!MyLettersParameter.Value, "a") > -1
The above code returns true or false. To return "1" use:
=IIf(Array.IndexOf(Parameters!MyLettersParameter.Value, "a") > -1, "1", "0")

Populating multiple values in rdlc reporting

I am using rdlc report, i have a column in database which i want to display in the report.
vehicleDamageArea=1,2,3
In the report I need to mark the placeholders with these values.
=iif((Fields!vehicleDamageArea.Value="3"),Chr(253),Chr(168)) like this.
But as we know,it will check the whole value 1,2,3="3" not the splitted values.
Any suggestion to check by splitting the vehicleDamageArea parameter.
I made it to work as below
Public Shared Function CheckValue(ByVal InString As String,ByVal input as String) As Char
Dim output As String = String.Empty
Dim Parts() As String = InString.ToString().Split(",")
For i As Integer = 0 To Parts.Length - 1
If Parts(i) = input Then
output = Chr(0120)
Exit For
Else
output = Chr(0111)
End If
Next i
Return output
End Function
You can get the individual values using the split function in reporting services. It returns a zero-based string array, so for your example you need
=Split(First(Fields!ID.Value),",")(2)
You should make a function that accept a comma separated expression, than process this string and return a Boolean, then call this function as for boolean value.