为什么将数据类型varchar转换为bigint时出现此错误?

时间:2016-08-17 08:58:17

标签: sql sql-server sql-server-2008 sql-server-2012

为什么我会遇到这个错误?这非常烦人,并尝试解决它但不起作用。

  

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

该值 2000100020502 会引发错误。

pkg-resources

3 个答案:

答案 0 :(得分:1)

您可以使用try_convert函数。我假设您正在使用sql server 2012及更高版本.....

SELECT DATEADD(HOUR, 4, PF.EntryDateTime) 'EntryDateTime'
    ,C.CompanyName
    ,B.BranchName
    ,@FromDate AS 'Fromdate'
    ,@ToDate AS 'Todate'
    ,E.EmployerName
    ,RE.EmployeeName
    ,RE.EmployeeUniqueID
    ,FE.IncomeFixedComponent
    ,FE.IncomeVariableComponent
    ,S.StatusDescription
FROM File_EdrEntries FE
JOIN RegisteredEmployees RE ON RE.EmployeeUniqueID = FE.EmployeeUniqueID
JOIN Employers E ON E.ID = RE.Employer_ID
JOIN Branches B ON B.BranchID = E.Branch_ID
JOIN companies C ON C.COMPANYID = B.COMPANY_ID
JOIN Statuses S ON S.StatusID = FE.Status_ID
JOIN PAFFiles PF ON PF.ID = FE.PAFFile_ID
WHERE PF.EntryDateTime >= @FromDate
    AND PF.EntryDateTime < DATEADD(DAY, 1, @ToDate)
    AND Status_ID IN (
        1
        ,2
        )
    AND E.Branch_id IN (
        SELECT BranchID
        FROM @Branches
        )
    AND try_convert(BIGINT, E.EmployerID) = ISNULL(try_convert(BIGINT, @EmployerUniqueID), try_convert(BIGINT, E.EmployerID))
    AND try_convert(BIGINT, RE.EmployeeUniqueID) = ISNULL(try_convert(BIGINT, @EmployeeUniqueID), try_convert(BIGINT, RE.EmployeeUniqueID))

如果try_convert函数适用于您的情况,那么您的数据包含字母数字字符。

答案 1 :(得分:0)

可能是在投射列而不是变量时: CAST(E.EmployerID AS BIGINT)CAST(RE.EmployeeUniqueID AS BIGINT)。 您确定所有Employers.EmployerIDRegisteredEmployees.EmployeeUniqueID都可以投放为bigint吗?

答案 2 :(得分:0)

select convert( bigint , 2000100020502)

It is fine here in SQL Server 2012