How to replace Null fields with 0s - reporting-services

I have a field in a matrix that has some nulls. I would like to replace all the nulls with 0s. This is the expression I currently have in the field:
=Sum(IIf(Code.isVM(Fields!deviceType.Value), 0, 1))
I'm guessing I have to have an IsNothing expression in there but I can't figure out how to add this with this existing expression.

How about:
=Iif(
Sum(IIf(Code.isVM(Fields!deviceType.Value), 0, 1)) Is Nothing,
0,
Sum(IIf(Code.isVM(Fields!deviceType.Value), 0, 1))
)

I recall testing for length greater than 0 worked well.
=IIf(Len(Fields!deviceType.Value) > 0, Fields!deviceType.Value, 0)

Related

MS Access Case sensitive query giving incorrect result

Why do these queries give different results? Reference is a single character column and I would expect to have a result giving counts for upper and lower case letter 'r'.
Select SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
The result is that both BIG_R and LITTLE_R are the same and equal the count of BIG_R's
However,
Select SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTE_R,
SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Again LITTLE_R and BIG_R are the same, but this time they equal the count of LITTLE_R's
This looks like a bug in the way MS Access processes this type of query, or have I missed something here?
Access (or probably rather JetEngine) thinks that StrComp is called twice with the same argument and optimizes away one of the two calls.
A workaround is to compare the ASCII character values (Asc("r") = 114, Asc("R") = 82):
Select
SUM(IIF(Asc([REFERENCE]) = Asc('R'), 1, 0)) AS BIG_R,
SUM(IIF(Asc([REFERENCE]) = Asc('r'), 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Yet another workaround:
Select SUM(IIF(StrComp([REFERENCE],Chr$(82),0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],Chr$(114),0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Here the two inputs to StrComp are clearly different. So, the second call not optimized away.

MySQL Tagging Rows in Sequence based on a pattern

I have a column which I am trying to convert in MySQL into another column with a pattern where ever there are consecutive 1s in the data. Please see the example dataset below
Dataset Sample: https://1drv.ms/x/s!ApGNZAoiMmX3gi9OR7SUxt3ou84v?e=tuSV7f
Following is the code I have written but not able to make it work and any suggestions would be helpful.
select rownum,result,movingsum,new_result
(select rownum,result,movingsum,
if(result_norm_max=0,0,if(movingsum=1,1,0)) as new_result
from
(select rownum,result,
sum(result) over (order by rownum rows between 2 preceding and current row) as movingsum
from mytable) a;
The issue is, the above code doesn't return the output needed for all required logic of:
when result column is 0 new_result should be 0
when result is 1, new_result = 1 but only when previous 2 new_results are 0
Any suggestion on how I should approach this will be useful.
Thanks!
With some tries I was able to find the solution which is close to what I need as mentioned below. I used 2 variables to carry out the trick,
select rownum,result,
if (result= 0, 0, if(#n = 1, if(#m >= 7, 1 , 0), 1)) as new_max,
if (result= 0, 0, if(#n = 1,
case when #m >= 7 then #m:=0 else 0 end
, 1)) as new_max1,
if (result= 0, if(#m>0,#m:=#m-1,#m:=0), if(#n = 1, #m:=#m+1,#m:=#m-1)) as new_m,
#n := result
from mytable a, (select #n:= 0, #m:= 0) b

SSRS Divide by Zero error on totals with weighted average

When attempting to get a total weighted average interest rate I occasionally receive Error when there is only one item in certain columns. Having trouble with the Iif statement handling this:
=Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 0, SUM(Fields!Current_Principal_Balance.Value * Fields!WAIR.Value))/Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 1, SUM(Fields!Current_Principal_Balance.Value))
Moved your brackets slightly, this seems to work:
=Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 0,
SUM(Fields!Current_Principal_Balance.Value * Fields!WAIR.Value)/Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 1, SUM(Fields!Current_Principal_Balance.Value)))

How to format to display leading zero

I am trying to display the duration in this format: 05:02:09 which is hour, minute and second.
At the moment I can display it without the leading zero in this format: 5:02:09
=IIF(
Fields!DataValue.Value < 0, 0,
Floor(Fields!DataValue.Value / 3600) &":"&Format(
DateAdd("s",IIF(Fields!DataValue.Value < 0, 0, Fields!DataValue.Value),"00:00"),
"mm:ss"
)
)
How can add a leading zero when the hour is less than 10?
I found a solution, which I tried before but failed. Strangely, it worked this time.
=IIF(
Fields!DataValue.Value < 0, 0,
Format(Floor(Fields!DataValue.Value / 3600), "00") &":"&Format(
DateAdd("s",IIF(Fields!DataValue.Value < 0, 0, Fields!DataValue.Value),"00:00"),
"mm:ss"
)
)

#Error in a greater than iif sum statement

I'm trying to write some code that will prevent a #Error.
I don't know where my issue is.
=Sum(IIF(Fields!CurrentNumDaysOD.Value >= 1 And Fields!CurrentNumDaysOD.Value <= 10,
Fields!CurrentBalance.Value, "0")) / Sum(IIF(Fields!CurrentNumDaysOD.Value > 0,
Fields!CurrentBalance.Value, "0"))
I have also attempted this:
=Sum(IIF(Fields!CurrentNumDaysOD.Value >= 1 And Fields!CurrentNumDaysOD.Value <= 10,
Fields!CurrentBalance.Value, Nothing)) / Sum(IIF(Fields!CurrentNumDaysOD.Value > 0,
Fields!CurrentBalance.Value, Nothing))
If the result of the second half of your statement (after the /) is 0, then you will be dividing by 0, which will cause an error.