SQL Reporting Services empty string handling - sql

I'd like to display a string without the last 2 chars in a text field in Reporting Services 2005 vs2005.
I tried several ways and if the string is empty or null I get an error:
rsRuntimeErrorInExpression - The value expression for the textbox contains an error: Argument 'Length' must be greater or equal to zero.
Here are the ways I tried:
IIF(trim(Fields!kuku.Value) = "","", Left(Fields!kuku.Value, Len(Fields!kuku.Value) - 2))
IIF(IsNothing(Fields!kuku.Value) and Len(Fields!kuku.Value) = 0,"",Left(Fields!kuku.Value, Len(Fields!kuku.Value) - 2))
IIF(IsNothing(Fields!kuku.Value) ,"",Left(Fields!kuku.Value, Len(Fields!kuku.Value) - 2))
IIF(Len(Fields!kuku.Value) = 0,"",Left(Fields!kuku.Value, Len(Fields!kuku.Value) - 2))
Any ideas as to what I'm doing wrong? Thanks in advance.

how about changing the dataset on that field to use isnull(field,", ") that way you can always safely trim 2 characters.
or
IIF(IsNothing(Fields!kuku.Value) OR Len(Fields!kuku.Value) < 2,"",Left(Fields!kuku.Value, Len(Fields!kuku.Value) - 2))
note the and changed to an OR. And just in case the length is 1 changed =0 to <2

Related

How to work with foldRight() in Kotlin?

I try this code
println(listOf(1, 2, 4).foldRight(0) { total, next ->
total - next
})
I thought it works like 0 + 4 - 2 - 1 = 1.
But it returns 3. Why?
Sorry for this silly question.
foldRight works by accumulating the result from right to left. In your case, it is going to do
(1 - (2 - (4 - 0))) = (1 - (2 - 4)) = 1 - (-2) = 3
Note that your operation has its parameters in the wrong order, foldRight will pass you the next element as the first parameter and the accumulator as the second parameter. See https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold-right.html. If you swap them, you would have
(((0 - 4) - 2) - 1) = - 7
unless I am getting something wrong

Substring function does not work in Vb?

I am trying to mask SSn and want show it on label caption.
lblSPTINTo.Caption = rsMM("SPTIN")
lblCPTINTo.Caption = rsMM("CPTIN")
i am trying to use substring function to get last 4 characters but i not am to able to use it as it throws compile error .
lblSPTINTo.Caption = rsMM("SPTIN").sutbstring(4,4)
Replace sutbstring with Substring.
But it won't work that way because the first parameter is the index and the second parameter in Substring is the length, if you want the last 4 characters:
Dim last4 As String = rsMM("SPTIN")
If last4.Length > 4 Then last4 = last4.Substring(last4.Length - 4)

Sum variables in VBA

I have found myself into an issue that looked so simple and stupid at the beginning but keeps me struggling to solve it for over 24 hours now.
I have a string (bunch of numbers delimited by |) that I want to be converted into array and then sum some of the array keys depending on the case.
The first issue I have found was the Integer length limitation, I couldn't believe when VBA was unable to return a number higher than 32767 (Then I found longs...). After "solving" that I found that when trying to SUM some 0 values it actually increase my grand total and I can't find any explanation for this.
Below you can see what I have now:
Public Function calcTime(TimeType As String)
Dim jsSting As String
Dim strSplit As Variant
Dim tempTime as Double
jsSting = "100|0|10080|400|0|4320|70|0|1440|30|0|2280|10|0|7400|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|300|0|15855|90|0|1721"
'Split the string by delimiter
strSplit = Split(jsSting, "|")
Select Case UCase(TimeType)
Case "TOTAL"
tempTime = WorksheetFunction.Sum(strSplit(2), strSplit(5), strSplit(8), strSplit(11), strSplit(14), strSplit(17), strSplit(20), strSplit(23), strSplit(26), strSplit(29), strSplit(32), strSplit(35), strSplit(38))
Case "GROUP1" ' Team 1 + Team2
tempTime = WorksheetFunction.Sum(strSplit(2), strSplit(5), strSplit(8), strSplit(11), strSplit(14))
Case "GROUP2" ' Team 1 + Team2 + Team3
tempTime = WorksheetFunction.Sum(strSplit(2), strSplit(5), strSplit(8), strSplit(11), strSplit(14), strSplit(38))
Case "GROUP3" ' Team 5
tempTime = WorksheetFunction.Sum(strSplit(17), strSplit(20), strSplit(23), strSplit(26))
Case "GROUP4" ' Team 2
tempTime = strSplit(14)
Case "GROUP5" ' Team 6
tempTime = WorksheetFunction.Sum(strSplit(29), strSplit(32), strSplit(35))
End Select
Return tempTime
End Function
In this example I have tried to use Excel's SUM function in order to get the desired result but It wasn't a success.
Sticking to the TOTAL case. It sums the following keys - values:
jsString(2) - 10080
jsString(5) - 4320
jsString(8) - 1440
jsString(11) - 2280
jsString(14) - 7400
jsString(17) - 0
jsString(20) - 0
jsString(23) - 0
jsString(26) - 0
jsString(29) - 0
jsString(32) - 0
jsString(35) - 15855
jsString(38) - 0
This gives a total of 41375, however, when I do the sum in VBA I get 43096 and I can't understand why. If I remove from the SUM the values with 0 it returns the correct 41k value.
Hope this makes sense and the answer is simple (I am seriously thinking that I've missed something when assigning the data type).
Thank you in advance for your help !
Did you maybe mean ... ?
Select Case UCase(TimeType)
Case "TOTAL"
tempTime = WorksheetFunction.Sum(strSplit(2), strSplit(5), strSplit(8), strSplit(11), strSplit(14), strSplit(17), strSplit(20), strSplit(23), strSplit(26), strSplit(29), strSplit(32), strSplit(35), strSplit(38)
Anyway, the problem is that you're summing also strSplit(38) which is 1721 even if you wrote in your sample which is equal to 0, exactly the difference between Excel and VBA ;)
Check with a MsgBox strSplit(38) in your code.
strsplit(38) = 1721, which is the difference of 43096 and 41375

How to convert string to byte in Visual Basic

I'm trying to simulate an algoritham in cryptography and I need to convert a string of 0s and 1s back into a word. Example:
I have: 01011110010101101000001101100001101
I have split it into an array of strings:
0101111, 0010101, ...
each member has 7 characters. I want to get a letter that 0101111 represents in UTF8? How do I do this?
I try CType("0010101", Byte), but it fails. I can pass max 111 this way.
Help :/
UTF-8 is 8 bit, those are only 7 bits. Do you mean 7 bit ASCII?
In that case here you go:
Function BinToStr(binStr As String) As String
Dim i As Long
For i = 0 To (Len(binStr) / 7) - 1
[A1] = CLng(Mid(binStr, i * 7 + 1, 7))
BinToStr = BinToStr & Chr([BIN2DEC(A1)])
Next
End Function
If that's not what you're looking for, let me know.

SQL function with square roots causes problems

I have a query that is erroring out:
SET #SumDiff = #SitesSum - (select power(SUM(Sums),2)/#NumSites from #SiteSums)
SET #SE = power((#SumDiff/(#NumSites - 1)),0.5)/power(#NumSites,0.5)
This function works most of the time, but I get an error some times.
Here are the values when i have the problem.
SUM(Sums) = .01 so squared = .001
#NumSites = 2
#SiteSum = 0
The value from this part:
select power(#Sums,2)/#NumSites from #SiteSums
comes out as 5E-05 which would match the value expected, .00005
When I try to set #SE it's basically finding the square root of (.00005 / 1)/ square root of 2 (1.4142135623731).
Should be something like 3.535533905932725. But I get the message:
Msg 3623, Level 16, State 1, Line 306
An invalid floating point operation occurred.
Is .00005 too small to divide in SQL? Could I do a cast? This isn't always going to be working with decimals, mostly larger numbers.
Actually, using your values this:
SET #SumDiff = #SitesSum - (select power(SUM(Sums),2)/#NumSites from #SiteSums)
..results in #SumDiff being set to -5E-05, and not 5E-05.
So when you then do:
power((#SumDiff/(#NumSites - 1)),0.5)
..you are trying to take the square root of a negative number which is invalid (undefined) for a Real number.
The ABS(..) function can fix this for you:
SET #SE = power((ABS(#SumDiff)/(#NumSites - 1)),0.5)/power(#NumSites,0.5)