SSRS expression replace NULL with another field value - reporting-services

I need to write an SSRS expression to check and replace NULL field value with another field value. Can this be done?

=iif(isNothing(Fields!FV1.Value), Fields!FV2.Value, Fields!FV1.Value)

If you have to do it a bunch of times, you can also make a reusable function to avoid a lot of typing. Here's a solution modeled off of SQL's ISNULL function:
Right click on the Report Document and go to Report Properties.
Navigate to the Code tab and add the following function:
Public Function IsNull(input As Object, defaultValue As Object) As Object
Return IIf(input Is Nothing, defaultValue, input)
End Function
Note - Even though the custom code is expecting valid VB.NET code, you have to use the IIF Ternary operator.
Then you can use it in an expression like this:
=Code.IsNull(Fields!MyField.Value,0)

Related

SSRS function needs to read field from additional dataset

So in the custom code in SSRS this function is reading a field from a different data set. To be specific the field is "TOTAL_WORK_HOURS".
Public Function CRFtotalhourswobreak(Fields As Microsoft.ReportingServices.ReportProcessing.ReportObjectModel.Fields) As Double
Return Fields!TOTAL_WORK_HOURS.Value - CRFbreakhours(Fields)
End Function
Because that field is in a dataset not being used by the Tablix, the field simply isn't being read. I know this to be true because whenever I replace the field with random number, I do get results.
Is there proper syntax to implement a field that isn't in the main dataset?
you can use
=First(Fields!TOTAL_WORK_HOURS.Value, "DataSet1")

the expression you entered contain invalid syntax in access query

I am trying to make a query. I don't type anything. I use only every formula by clicking in built-in function of access, but I get this popup error
The function iif requires the condition, iftrue, and iffalse to be separated with a comma, not a colon. Makr it look like this:
Iif(condition, ifTrue, ifFalse)

Crystal Reports Custom IsNull Function

I am trying to create a custom IsNull Function in Crystal Reports; the function must act the same way as the IsNull Function in MS SQL Server. I want to specify a field, and if the field is null, then it must be returned with a value I have specified.
IsNull({myField},0) or
IsNull({myField},'Hello World')
I have encountered that I have to create a separate function for number fields and a separate function for text fields. I also found that Crystal does not allow the use of standard functions inside of a custom function, for instance the ISNULL Function:
Function(NumberVar param, Numbervar setter)
IF ISNULL(param) THEN setter ELSE param
and
Function(StringVar param, StringVar setter)
IF param = NULL THEN setter ELSE param
Does anyone know how I can create a function like this in Crystal and a work around for the ISNULL inside of a custom function?
You can't pass a null value into a custom function, so it's pointless to use crystal's isnull function inside one. Only option is to write it like...
if isnull({myField}) then 0 else {myField}
I found this issue, in the formula editor there is a drop down in the header that indicates:
Exception for Nulls
Default values for nulls
Select the second one ( Default values for nulls)
I've encountered the same behavior, but I have yet to see a documented reason for this.
I would suggest that you use a SQL Expression:
//{%myField}
(
ISNULL({myField},'Hello World')
)
This worked for me:
if (isnull({dbvalue}) or ({dbvalue} ='')) then
"Display the required text"
else
{dbvalue}

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

RDLC Expression Extension Methods

Is it possible to write extension methods for expressions behind RDLC fields?
For example, let's say that I have a DateTime field in my datasource that may either have a valid value or may be null. I drag and drop a TextBox onto my RDLC and format its value using the ToShortDateString() method. This works fine for populated DateTime value, but this will also obviously throw an exception at runtime if I try to do a .ToShortDateString() on a NULL field.
I was wondering if I could write an extension method that I could use in my RDLC expressions so that when I'm dealing with ?DateTime values, I could call a method like .ConvertFromNullToEmptyString().
Of course there are other ways to work around this issue, but I was wondering if extension methods for use in RDLC expressions would be a possible approach to my business problem.
Thanks folks!
Yes, this is possible. You can either embed code directly in the report or include a custom assembly.
It is possible to use extension methods, but not AS extension methods on an instance of an object. You would have to call them as a static method call on the type of which they are a member. So instead of myDictionary.Values.Sum() -- calling the Sum method on the Values property of a dictionary instance -- you could use System.Linq.Enumerable.Sum(myDictionary.Values) -- passing the instance into the static Sum method of the Enumerable type (in this example, the report must reference the System.Core assembly). So yes, you can use methods that are also extensions, but (it appears anyway) not as extensions on a particular instance.
While I agree with Corina on the solution to the question, I believe a better solution can be reached without going the route she suggests, using built in expressions. In any case where you have a DateTime coming from SQL, you're correct, it can be null, however, you can easily test for this using an IIF statement (remember that the expressions are basically in VB) to check for null / nothing / empty and as long as it is something, run the desired operation, otherwise return blank. Just be careful, as the resulting type of the IIF will probably be string.
For example, let's say that I have a DateTime field in my datasource that may either have a valid value or may be null. I drag and drop a TextBox onto my RDLC and format its value using the ToShortDateString() method. This works fine for populated DateTime value, but this will also obviously throw an exception at runtime if I try to do a .ToShortDateString() on a NULL field.
There should be no need for a custom function in your case. Just use VB's If() ternary operator:
=If(Fields!MyDate.Value IsNot Nothing, Fields!MyDate.Value.ToShortDateString(), "N/A")
(Personally, I funnel my objects through AutoMapper and let it substitute null values with default values or objects, so that I don't have to deal with null values in the report at all).
It should be mentioned, that the If(condition, true_part, false_part) ternary operator should (with one I) be preferred over the IIf(condition, true_part, false_part) function (with two I's) in most cases.
The If() ternary operator will short-circuit evaluate only the part that corresponds to condition outcome (e.g. a condition that is True will only evaluate the true_part).
The IIf() function will always evaluate the condition and both parts, because it is just a function call and all parameters of the function will be evaluated before the call.