您好我有一个excel VB程序从数据库中提取信息。我的相关功能代码如下。此代码WAS工作多年,现在我收到一个错误:“将数据类型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
答案 0 :(得分:1)
如果以某种方式将非数字值插入到sCCNumber
字段中,就会发生这种情况。修复方法是找到该值并修复它。
或者你可以替换
and sCCNumber <> ''
与
and ISNUMERIC(sCCNumber) = 1
答案 1 :(得分:0)
正如詹姆斯指出的那样,你可能有无效的数据。也许是一个额外的领先空间,或网络不间断的空间,或逗号。
我会改变最后一行,因为你需要它是12位数。
sSQL = sSQL & " and sCCNumber not like '%[^0-9]%' and len(sCCNumber) = 12"