iFunctionConvertPos = strSQL.ToLowerCase.IndexOf("iif(")
            strError = "Error in function Iif"
        
            Do While iFunctionConvertPos > -1
                iFunctionConvertEndPos = GetPosOfClosingBracket(strSQL, iFunctionConvertPos)
                strFunctionToReplace = strSQL.SubString2(iFunctionConvertPos, iFunctionConvertEndPos + 1)
                iCommaPos = GetSQLFunctionCommaPos(strFunctionToReplace, 0)
                If iCommaPos = -1 Then Exit 'so leave SQL as it is and cause a handled error
                strArg1 = strFunctionToReplace.SubString2(4, iCommaPos)
                iComma2Pos = strFunctionToReplace.IndexOf2(",", iCommaPos + 1)
                If iComma2Pos = -1 Then Exit 'so leave SQL as it is and cause a handled error
                strArg2 = strFunctionToReplace.SubString2(iCommaPos + 1, iComma2Pos).Trim
                strArg3 = strFunctionToReplace.SubString2(iComma2Pos + 1, strFunctionToReplace.Length - 1).Trim
                strSQL = strSQL.Replace(strFunctionToReplace, _
                "Case When " & strArg1 & " Then " & strArg2 & " Else " & strArg3 & " End")
                iFunctionConvertPos = strSQL.ToLowerCase.IndexOf("iif(")
            Loop
Sub GetPosOfClosingBracket(strString As String, iStartPos As Int) As Int
    
    Dim i As Int
    Dim iOpenCount As Int
    
    For i = iStartPos To strString.Length
        If strString.CharAt(i) = "(" Then
            iOpenCount = iOpenCount + 1
        Else
            If strString.CharAt(i) = ")" Then
                iOpenCount = iOpenCount - 1
                If iOpenCount = 0 Then
                    Return i
                End If
            End If
        End If
    Next
    
    Return -1
    
End Sub