Duration = isnull(FunctionA(DateA,DateB),'')
The Function above calculates number of days and if day is null it displays the value 0 instead of blank value
How can I change the above code to so that it shows blank and not 0 for value null?
Change empty cell display Select the For empty cells show check box. In the box, type the value that you want to display in empty cells. To display blank cells, delete any characters in the box. To display zeros, clear the check box.
When you reference a cell in Excel, the cell reference can be blank. This means that the cell doesn't contain any data. If the cell is blank, Excel will return a zero (0) value. You can use this feature to your advantage to make sure that cells with no data don't affect your formulas.
To change a zero to a dash, you have to play on the 3rd argument of the code of a number. So, by adding just a dash between quotation marks in the 3rd parameter, all your 0s will be replaced by dashes.
If your function returns an integer the result from isnull
will also be an integer. In the case the return value is null
you will have an implicit conversion to integer for '' and that will be 0.
Try this:
declare @xx int
select isnull(@xx,'')
Result:
-----------
0
You can have the space if you first cast the return value from your function to varchar
.
declare @xx int
select isnull(cast(@xx as varchar(10)),'')
Result:
----------
.
If your function returns 0
instead of null
you can use nullif
to get a null value before you cast to varchar
.
declare @xx int = 0
select isnull(cast(nullif(@xx, 0) as varchar(10)),'')
Summary:
You need this:
Duration = isnull(cast(FunctionA(DateA,DateB) as varchar(10)),'')
or this
Duration = isnull(cast(nullif(FunctionA(DateA,DateB), 0) as varchar(10)),'')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With