SSRS and null datetime - reporting-services

I always thought that when a stored procedure brought back a null datetime that an SSRS report would show mindate in that cell. The thing is, in my experience this is not always true. I can't figure out why. Sometimes a report will show blank and sometimes mindate for a null datetime. Why is that?
Is there rules or documentation (that makes sense) that explains how this works? It's hard to be consistent when the tools you are using are not consistent. I can make two reports that are set up exactly the same way. One will show mindate and one will show blank for null datetimes. I don't get it.
EDIT:
First of all, yes the mindate thing has happened to many.
https://www.google.com/search?num=50&q=ssrs+null+date+show+blank&oq=ssrs+null+date+show+blank&gs_l=serp.3...8759.9774.0.9941.10.10.0.0.0.0.152.888.8j2.10.0....0...1c.1.25.serp..8.2.166.kl2WBVx4Ijw
Almost every result has someone talking about the mindate when they want a blank. Many of the results are from Stack Overflow so before you start telling me it never happens, realize you are flat wrong. You may have never seen it. I have never seen a severed finger in my food in all my years of eating and thousands of meals. Doesn't mean it hasn't happened to someone.....
Example of one that is showing min date:
In the report the expression for the text box is:
=Fields!SecondaryInjuryRecordDate.Value
The number formatting was set to Date->01/31/2000 in the placeholder properties window.
Pretty straightforward, nothing weird going on there. RIGHT?
In the proc, the code for that column is:
CASE WHEN ISNULL(l.SecondaryInjury, 0) = 0 THEN '' ELSE l.SecondaryInjuryDXDate END AS SecondaryInjuryRecordDate
That resulted in mindate being shown whenever SecondaryInjuryDXDate was null. I had to switch to this:
CASE WHEN ISNULL(l.SecondaryInjury, 0) = 0 THEN '' ELSE ISNULL(l.SecondaryInjuryDXDate, '') END AS SecondaryInjuryRecordDate
...to get blanks when the date was null.
SecondaryInjuryDXDate is a DATETIME and
SecondaryInjury is a bit.

Within SSRS, by default a NULL DateTime value will be represented as an empty string instead.
If data formatting is applied to the value, a MIN DateTime value will be used in place of the null value e.g.: converting the DateTime to a ShortDate as described:
The number formatting was set to Date->01/31/2000 in the placeholder properties window. Pretty straightforward, nothing weird going on there. RIGHT?
To avoid this, the column value must be wrapped in an expression to conditionally apply the required formatting:
=IIf(IsNothing(Fields!DateTimeField.Value), "", FormatDateTime(Fields!DateTimeField.Value, DateFormat.ShortDate))

Use something like this in textbox expression:
=IIf(IsNothing(Fields!DateTime.Value), "", Fields!DateTime.Value)

It looks like there is no documentation to be found about this. The next best thing is to have this Q&A document things based on our collective experience.
Supposing the question: "What does SSRS do when displaying a DATETIME with a NULL value?"...
...the answer is it will display an empty string instead. There is no reproducible scenario where a NULL value would be displayed as anything else, unless you do some work to that end yourself, for example:
If the query COALESCEs the NULL to something else, e.g. the string "NULL" or a MIN DATETIME;
If you have an expression for the textbox value, e.g. IIF(Fields!MyDateTime.Value Is Nothing, "01-01-1900", Fields!MyDateTime.Value)

Every field returned in a SQL query gets a datatype assigned. Generally, you want to keep dates returned as dates from SQL, not as strings. (Don't format the date to a string in SQL. Do that as close to the presentation layer as possible.)
So instead of this in your query:
CASE WHEN ISNULL(l.SecondaryInjury, 0) = 0 THEN '' ELSE l.SecondaryInjuryDXDate END AS SecondaryInjuryRecordDate
I would try to use:
CASE WHEN ISNULL(l.SecondaryInjury, 0) = 0 THEN CAST( NULL AS DateTime ) ELSE l.SecondaryInjuryDXDate END AS SecondaryInjuryRecordDate
See SQL cast datetime for an explanation of why you are getting 0 cast to a date.

I did find when I format an entire column such as FormatDateTime(field, DateFormat.ShortDate) - SSRS will format that blank field to 01/01/0001. Just an FYI.

Related

Row visibility expression using ReportItem collection not working

This is major edit from the previous version of this question, which would hold little of value for future readers anyway.
I have a report where I want to hide certain rows depending on the data in certain fields, but also be able to override this based on a parameter value.
So for my visibility (Hidden), I am using the following expression:
=iif(CDbl(ReportItems!tbOEM1.Value) >= 20 Or CDbl(ReportItems!tbOEM2.Value) >= 20 Or CDbl(ReportItems!tbOEM3.Value) >= 20 Or Parameters!OEM20.Value.Equals(False)
, False
, True
)
Which I think should mean, if any of the 3 tbOEM fields are >= 20, then display the row. But if the user has selected False for the "OEM20" parameter (Boolean), display all the rows regardless of the values.
However, when I run the report and choose True for the OEM20 parameter, No rows get displayed, even though I know that there are rows that have OEM values over 20.
To investigate, I added a background color expression to each of the tbOEM reportitems. Here is the background color expression for tbOEM1:
=iif(CDbl(ReportItems!tbOEM1.Value) >= 20
, "Red"
, "White"
)
When I run the report with False for the OEM20 parameter, I see all rows returned, and no fields are colored Red, even the ones that should be because they are tbOEM fields with a value >=20.
So I wonder if this is an order of execution issue where the value of the ReportItem is not yet known when setting visibility and background color? Is this known and documented anywhere? I googled and couldn't find anything.
Or am I doing something else wrong that is fixable?
I already know that a workaround is to use the datafield calculations that populate the tbOEM fields in the expressions, rather than the ReportItems collection. So you needn't bother to tell me about this option.
Here's some things to try. Putting this as an answer as it will be too big for a comment.
I'm still thinking something weird is happening with type comparison/conversion.
Maybe the tbOEM textboxes are strings with non-numeric characters (even just a space) so the CDbl conversion is failing, and thus failing the CDbl(ReportItems!tbOEM1.Value) >= 20 test even though it looks like it is above 20 when displayed as a string. Given your background colour expression is also failing, this seems to be the case. Let's use the Val expression for the comparison instead:
Val(ReportItems!tbOEM1.Value) >= 20
Val doesn't give errors when converting to a numeric value, it just does the best it can and returns whatever number it finds. Convert the field into the numeric value for display purposes as well as for the background colour expression so we see what the expression sees, not just the displayed text. On your tbOEM1 textbox use these expressions for the following properties:
Value
=Val(Fields!OEM1.Value)
Background.Color
=iif(Val(ReportItems!tbOEM1.Value) >= 20, "Red", "White")
If this still fails (for example, the textbox no longer displays a value) then let's trim it first to get rid of any spaces: =Val(Trim(Fields!OEM1.Value))
If the OEM1 field is a decimal value use 20.0 rather than 20 so it is comparing to a Double rather than an Integer in case something funky is happening with the implicit type conversion in the comparison.
Okay, now hopefully the textboxes are displaying the correct number and the background is being coloured correctly. Now we just need to fix the Visibility.Hidden expression for the row. I wouldn't use Value.Equals here as the parameter is already a boolean so we can just use it directly:
=iif(Val(ReportItems!tbOEM1.Value) >= 20 Or Val(ReportItems!tbOEM2.Value) >= 20 Or Val(ReportItems!tbOEM3.Value) >= 20 Or Not Parameters!OEM20.Value
, False
, True
)
This needs to be applied to the entire row by selecting the handle for the row on the left of the tablix, rather than applied to the textboxes directly.
Of course, add the Trim function if the experiment above required it.
Good luck! I hope this helps.

Sum Values not equal to a space from a Control Source in MS Access

As the subject expresses, I'm trying to sum the values of a string field where spaces may exist. It must be done this way, unfortunately.
The database is very old. The original developer chose to make all fields Text fields; to get over the null value problems, a function was written in VB6 to replace any null value with a space. This cannot be changed.
Fast forward to now, I'm trying to create a report that sums the length field without changing spaces to nulls first, and it should be done entirely through the control source property of the report.
I've added some of what I've tried below, but every time the report is run, I receive:
Data Type Mismatch
...and I'm not sure how to get around it.
Ideally, I'd like to keep the users out of the database completely, and just add a combo box that lists the reports created in the database so they can be opened by name without having to run any additional update queries first.
=Sum(IIf([MY_LEN]<>" ",DCount("[MY_LEN]","MY_TABLE"),0))
=Sum(Nz(Iif(Trim([MY_LEN])='',Null,[MY_LEN]),0))
=DSum("[MY_LEN]","[MY_TABLE]","[MY_LEN]<>' '")
=Sum(Iif(Val([MY_LEN])>0,[MY_LEN],0))
=(SELECT Sum([MY_LEN]) AS MyLen FROM MY_TABLE WHERE (((MY_TABLE.[MY_LEN])<>' ')))
Is this possible?
Can't compare anything to Null. Can't say If x = Null Then because Null is undefined. So you can't test if undefined = undefined. Use If IsNull(x) Then in VBA and Is Null in query criteria. Don't really need IIf() for Sum() aggregate, other aggregates such as Count or Avg would.
To handle possible space, empty string, or Null for a text field holding numeric data.
=Sum(Val([MY_LEN] & ""))

SSRS Iif always true when false part contains expression

I've got a report outputting to an Excel file, with some fields in a database being empty.
I'm trying to filter these null values out using the following Iif expression in a cell:
=Iif(
IsNothing(Fields!START_DATETIME.Value),
0,
Fields!START_DATETIME.Value
)
This doesn't work, I get "#VALUE" in the cells where the data was null, and the time value of the cell where there was data.
If I use the same expression without an expression as the third parameter:
=Iif(
IsNothing(Fields!START_DATETIME.Value),
0,
1
)
I get 1s where there is a date in the table and 0s where there is nothing.
I'm sorry if this is a duplicate, I couldn't find anything that worked for me...
Edit:
I'm using VisualStudio Professional 2013
Update
What I've got for each entry in the database is: a datetime for authorization time, another for start time and another for stopped time. There is always an entry for authorization time, but not always for start and stopped.
I've split the date and time out from the various datetimes into columns like so:
Authorized date
Authorized time
Start time
Stop time
The idea is that if no start and stop time are present, just use the authorized time
The following expressions are put in the Start time column. Sorry, I don't have enough rep to post images or more than 2 links. I've put program output in this album:
http://imgur.com/a/zJSAY
Doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value))
)
Outputs the authorized time, so that works fine.
However, doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!START_DATETIME.Value))
)
Outputs the start time where there is an entry, and #VALUE! (null or some such) when there isn't anything.
And doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!START_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value))
)
Outputs the authorized time where there is an entry in start time, and #VALUE! when there isn't anything.
I can't figure this out. I understand (1) and (3), but what I need is for (2) to display the correct start time while there is one, and the authorized time when there isn't.
Update 2
It turns out that Iif() evaluates each conditions even if it is not selected, and any error encountered is passed on regardless. See comments to Why isn't my iif statement evaluating true when the condition is correct?
To fix this I did:
=DateAdd(
DateInterval.Day,
693594,
TimeValue(
Iif(
Fields!START_DATETIME.Value is Nothing,
Fields!AUTH_DATETIME.Value,
Fields!START_DATETIME.Value
)
)
)
This seems to work for me, and should have been the common sense way to do this in the first place...
Thanks again for your help guys.
Excel will guess the type of a column based on the first few cells. If there's typical DATETIME values in those, it'll guess the column is a date column, and it will error with "#VALUE! on values that don't match the type, such as "0".
You should ask yourself what you want to display in Excel for NULL values:
If you just want an empty cell, don't use an expression at all. Just Fields!START_DATETIME.Value will render a NULL value as an empty cell.
If you want a "baseline" datetime, make sure to use that instead of "0". Easiest is probably in your query with something like ISNULL(START_DATETIME, GETDATE()).
If you really want a "0", make sure it appears in one of the first few cells by ordering. Excel will see the "0" and not set the column type to date/time.

SQL Reporting Services IIf statement evaluates both sides of if causing #ERROR

I am currently working on a report which has data injected into it from a Web Service. This webservice sends it a "double?" permitting null values. In the report it displays the values returned which are later used to sum and average the numbers. My issue is in the expression to display the value (and i can see a similar issue coming up when i work on the calculations but one step at a time...) I am using an IIf statement checking if the value is numeric, if so display the value (converting it to a double) if not display an empty string. When its a numeric value the value displays properly however when its null I am getting the value #ERROR. It seems that the IIf statement evaluates both ends of the IIf statement!!!
Anyhow I have done some research and it seems people suggest to modify the sending code to check if its null and send a 0 instead BUT in my case this wont help (nor will a negative number as it also sends negative numbers). The reason for this not working in my case is that I have to calculate an average using all 0s but not null values. For example...
"100, 0, null" => this should be an average of 50... if that null
where to be converted to 0 it would average 25...
Anyways heres my code...
=IIf(IsNumeric(Fields!Ventas.Value), CDbl(Fields!Ventas.Value), "")
I have also tried with a switch statement and get the same issue...
=Switch(IsNumeric(Fields!Ventas.Value) = False, "", IsNumeric(Fields!Ventas.Value) = True, CDbl(Fields!Ventas.Value))
Also I have tested that the IIf condition is working properly by testing this:
=IIf(IsNumeric(Fields!Ventas.Value), 1, 0)
And that works properly... Anyways any help would be greatly appreciated as I dont know what else to try... below are some links I found regarding my issue but again they recommend sending a 0 instead of null which in my case is useless... And one link suggests using a switch but again that didnt work...
Link #1
Link #2
I just solved this issue heres how it can be done hope this helps someone else with this same problem!!
=IIf(IsNumeric(Fields!Ventas.Value), CDbl(IIf(IsNumeric(Fields!Ventas.Value), Fields!Ventas.Value, 0)), "")
You will have the same issue using expression with possible division by zero, like IIF([B]=0, Nothing, [A]/[B])
I used a simple workaround: IIF([B]=0, Nothing, [A]/IIF([B]=0, 1, [B]))
This error is also registered on the Microsoft Connect, but no answer from MS so far.

Problem evaluating NULL in an IIF statement (Access)

Item in the recordset rstImportData("Flat Size") is = Null
With that, given the following statement:
IIF(IsNull(rstImportData("Flat Size")), Null, cstr(rstImportData("Flat Size")))
Result: Throws error 94: Invalid use of Null
If I change the statement by removing the type conversion upon a false comparison:
IIF(IsNull(rstImportData("Flat Size")), Null, 0)
Result: Null
It returns Null as it should have the first time. It appears that I cannot do a type conversion in an IIF if the value passed in should ever be null even if it passes an IIF test, it still attempts to evaluate it at both the true and false answer. The only reason I'm using IIF like this is because I have a 25 line comparison to compare data from an Import against a matching record in a database to see if I need to append the prior to history.
Any thoughts? The way data is imported there will be null dates and where the spreadsheet import is in a string format I must convert either side to the other to compare the values properly but if either side is null this exception occurs :(
EDIT
Example of why I was using IIF (and considering using a universal function)
If master("one") <> import("one") Or _
master("two") <> import("two") Or _
master("date") <> import("date") Or _ //import("date") comes from a spreadsheet, it comes in as string, CAN be a null value
master("qty") <> import("qty") Or _ //import("qty") comes from spreadsheet, comes in as a string can CAN be null
master("etc") <> import("etc") Then
....stuff....
End If
This code expands for roughly 20 columns to compare in the database. I would prefer to check as part of the statement. I can think of a bunch of solutions but they involve adding much more code. If that is the case power to it, however I'm not one to give in so easily.
Options I see are
Creating temp vars to do the work prior to comparing and using these new vars instead of the recordset
Creating an object to pass the record into to preformat and work with, though extra work would provide this functionality to each import type since there are different files with similar fields
I'm here for ideas, and I'm open to any interesting pieces that can be thrown my way as I get to decide how to do it I'm looking for the most reusable approach.
The simple expedient of changing the value to a string helps tremendously. The trick is that trimming a string which is NULL will get a null string. Which can then be operated on as if it wasn't a database null.
I frequently use the form:
CInt("0" & Trim(SomeVariant & " "))
To get a valid number without having to go through a bunch of hijinks. The null is a nonentity for this problem.
The behavior you described is the standard way IIf operates under VBA. This is part of what Access 2003 Help says about it:
"IIf always evaluates both truepart and falsepart, even though it returns only one of them. Because of this, you should watch for undesirable side effects. For example, if evaluating falsepart results in a division by zero error, an error occurs even if expr is True."
However, if you use an IIf statement in a query, evaluation short circuits after truepart when expr is True --- falsepart is not evaluated in that case. Unfortunately this information is not useful for you ... unless you can incorporate a query into your comparison.
I don't know of any other way to avoid your error with IIf. I would try appending the Excel data into a table whose structure matches that of the table you will compare against, thereby eliminating the need to do a string conversion at the same time you do the comparison.