简体   繁体   English

将数据类型nvarchar转换为bigint时出错

[英]Error converting datatype nvarchar to bigint

Hi I have a excel VB Program that pulls info from a database. 嗨,我有一个Excel VB程序,可以从数据库中提取信息。 My code for the function in question is below. 我对相关功能的代码如下。 This code WAS working for years, now i am getting an error: "Error converting datatype nvarchar to bigint" Why would this happen all of a sudden and what is the fix? 这段代码已经使用了好几年了,现在我遇到一个错误:“将数据类型nvarchar转换为bigint时出错”为什么突然发生这种情况,解决方法是什么?

Public Function GetGiftCardsRedeem(iColumn As Integer, sFrom As String, sTo As String)

    Dim rsResults As New ADODB.Recordset
    Dim sSQL As String
    Dim iRecordCount As Integer

    ' Assign the Connection object.
    rsResults.ActiveConnection = gcnnDB

    ' Extract the required records.
    sSQL = "Select Sum(dTotalAmount) as TotalAmount from GiftCardTransactions where dtCreated between '" & sFrom & " 00:00:00' and '" & sTo & " 23:59:59'"
    sSQL = sSQL & " and CONVERT(BigINT, sCCNumber) Between 800110110000 and 800110159999" '800110110000
    sSQL = sSQL & " and not sCCNumber is null and sCCNumber <> ''"


    rsResults.Open sSQL, gcnnDB, adOpenStatic, adLockReadOnly, adCmdText
    If Not rsResults.EOF Then
        'Let's Cycle through the records..
        'We need to move last and then move back first to get a correct recordCount.

        DoEvents: DoEvents

        Sheet1.Cells(MTD_FREEGIFTCARDS_QTY_ROW, iColumn) = rsResults.Fields("TotalAmount").Value
    End If
    rsResults.Close
End Function

It would happen if somehow a non-numeric value was inserted into your sCCNumber field. 如果以某种方式将非数字值插入到您的sCCNumber字段中,则会发生这种情况。 The fix would be to find that value and fix it. 解决的办法是找到并修复该值。

Or you could replace 或者你可以替换

and sCCNumber <> ''

with

and ISNUMERIC(sCCNumber) = 1

As James has pointed out, it's likely you have invalid data in there. 正如James所指出的,您那里可能有无效数据。 Perhaps an extra leading space, or the web non-breaking space, or a comma. 可能是多余的前导空格,或者是网络不间断空格,或者是逗号。

I would change the last line, since you need it to be 12 digits. 我将更改最后一行,因为您需要将其设置为12位数字。

sSQL = sSQL & " and sCCNumber not like '%[^0-9]%' and len(sCCNumber) = 12"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM