在SQL Server中将varchar值转换为datatype int时转换失败

时间:2014-12-18 06:30:28

标签: c# sql-server

我收到错误

  

将varchar值'ORB2.0000000001'转换为数据类型int。

时转换失败

你能帮我解决一下这个问题吗?存储过程在开发中工作正常但在另一台机器上我收到了该错误

ALTER PROCEDURE [dbo].[EMR_GetReportData](
    @FromDate DATETIME
    , @ToDate DATETIME
    , @Code varchar(20) = NULL
    , @ReportType INT
) AS
BEGIN
    IF(@ReportType = 7)
    BEGIN
        SELECT DISTINCT
            PAI.OP_NO
            , PAI.Pat_Name
            , CONVERT(VARCHAR, PVD.Visit_Date, 106) AS DOA
            , DM.Name AS DrName
            , D.department
        FROM
            dbo.Pat_Info PAI
            INNER JOIN dbo.Pat_Visit_Details PVD ON PAI.OP_NO = PVD.OP_No
            INNER JOIN dbo.departments D ON PVD.Department_id = D.department_id
            INNER JOIN dbo.DoctorMain DM ON PVD.DrName COLLATE Latin1_General_CI_AI = DM.Code
        WHERE
            PVD.[Type] = 'OPD'
            AND PVD.Visit_Date BETWEEN @FromDate AND @ToDate
    END
    ELSE IF(@ReportType = 8)
    BEGIN
        SELECT
            PAI.OP_NO
            , PAI.Pat_Name
            ,  CONVERT(VARCHAR, PVD.DOA, 106) AS DOA
            , DM.Name AS DrName
            , D.department
        FROM
            dbo.Pat_Info PAI
            INNER JOIN dbo.Pat_Visit_Details PVD ON PAI.OP_NO = PVD.OP_No
            INNER JOIN dbo.departments D ON PVD.Department_id = D.department_id
            INNER JOIN dbo.DoctorMain DM ON PVD.DrName COLLATE Latin1_General_CI_AI = DM.Code
        WHERE
            PVD.[Type] = 'IPD'
            AND PVD.DOA BETWEEN @FromDate AND @ToDate
    END
    ELSE IF(@ReportType = 9)
    BEGIN
        SELECT
            D.Cr_No
            , PAI.Pat_Name
            , D.Code
            , CONVERT(VARCHAR, D.Visit_Date, 106) AS Visit_Date
            , D.[Description]
        FROM
            dbo.Diagnosis1 D
            INNER JOIN dbo.Pat_Info PAI ON D.Cr_No = PAI.OP_NO
        WHERE
            D.Code = @Code
            AND D.Visit_Date BETWEEN @FromDate AND @ToDate
    END
END

1 个答案:

答案 0 :(得分:1)

您的varchar值包含ORB中的一些字符ORB2.0000000001,这些字符无法转换为int。删除错误从报告字符串中删除这些字符,然后转换为int 只有当它只包含int(0,1,2,,3,4,5,6,7,8,9)时,你才可以将任何字符串转换为整数 此外,如果您希望值的十进制比使用浮点数据类型。请参阅here c#

的一些数据类型手册