【问题标题】:Why can't I compare a vba string type and a SQL short text type?为什么我不能比较 vba 字符串类型和 SQL 短文本类型?
【发布时间】:2017-01-04 12:51:40
【问题描述】:

我有一个带有短文本字段的表格和一个返回字符串类型的函数。当我将该字段与函数的返回值进行比较时,得到以下错误:

标准表达式的数据类型不匹配。


表格

查询

UPDATE myTable
SET clean = ""
WHERE trimWebAddress(webAddress) in (
SELECT domain FROM genericDomains)

功能

Public Function trimWebAddress(ByVal address As String) As String
    Dim cleanAddress As String

    If IsNull(address) = False Then
        cleanAddress = address
        If InStr(1, cleanAddress, "no_domain_available") = 1 Then
            cleanAddress = ""
        ElseIf InStr(1, cleanAddress, "https://") = 1 Then
            cleanAddress = Replace(cleanAddress, "https://", "")
        Else
            cleanAddress = Replace(cleanAddress, "http://", "")
        End If
        If InStr(1, cleanAddress, "www.") = 1 Then
            cleanAddress = Replace(cleanAddress, "www.", "")
        End If
        If InStr(1, cleanAddress, "/") > 0 Then
            cleanAddress = Left(cleanAddress, InStr(1, cleanAddress, "/") - 1)
        End If
    Else
        cleanAddress = ""
    End If
    trimWebAddress = cleanAddress
End Function

【问题讨论】:

    标签: sql vba ms-access


    【解决方案1】:

    您的数据中可能有 Null 值。

    你可以这样修改:

    Public Function trimWebAddress(ByVal address As Variant) As String
    
        Dim cleanAddress As String
    
        If Nz(address) <> "" Then
            cleanAddress = address
            If InStr(1, cleanAddress, "no_domain_available") = 1 Then
                cleanAddress = ""
            ElseIf InStr(1, cleanAddress, "https://") = 1 Then
                cleanAddress = Replace(cleanAddress, "https://", "")
            Else
                cleanAddress = Replace(cleanAddress, "http://", "")
            End If
            If InStr(1, cleanAddress, "www.") = 1 Then
                cleanAddress = Replace(cleanAddress, "www.", "")
            End If
            If InStr(1, cleanAddress, "/") > 0 Then
                cleanAddress = Left(cleanAddress, InStr(1, cleanAddress, "/") - 1)
            End If
        End If
    
        ' Prevent zero-length output.
        If cleanAddress = "" Then
            cleanAddress = "NotToBeFound"
        End If
    
        trimWebAddress = cleanAddress
    
    End Function
    

    如果不允许使用零长度字符串,您可能必须更新为 Null:

    UPDATE myTable
    SET clean = Null
    WHERE trimWebAddress(webAddress) in (
    SELECT domain FROM genericDomains)
    

    【讨论】:

    • 是的。我还运行了一个更新查询,就像我的问题中的那个一样,但没有 trimWebAddress 函数,只有一些其他 where 子句,它起作用了。
    • 也许 SQL 不喜欢函数的零长度输出。请参阅编辑以了解解决此问题的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2019-02-16
    • 2013-05-13
    • 1970-01-01
    • 2020-12-25
    相关资源
    最近更新 更多