1

目前,我正在执行以下查询并收到上述错误消息:

SELECT dbo.qryOtherFieldDataVerifySource.ItemID,
       dbo.qryOtherFieldDataVerifySource.EDGRDataID,
       dbo.qryOtherFieldDataVerifySource.LineItemID,
       dbo.qryOtherFieldDataVerifySource.ZEGCodeID,
       dbo.qryOtherFieldDataVerifySource.DataValue,
       dbo.tblBC.AcceptableValues,
       dbo.qryOtherFieldDataVerifySource.DataUnitID,
       dbo.qryOtherFieldDataVerifySource.DataDate,
       dbo.tblBC.DataTypeID,
       CASE
         WHEN DataTypeID = '{5951994B-BF47-4117-805D-B8F85FAB76A8}'
              AND ISNUMERIC(DataValue) = 1 THEN ( CASE
                                                    WHEN CAST(DataValue AS FLOAT(8)) >= 0 THEN 1
                                                    ELSE 0
                                                  END )
         ELSE 0
       END AS ValidPositiveNumericValue,
       CASE DataTypeID
        WHEN '{A6317BA5-F8FB-4866-A26B-24594650C2DC}'THEN ( CASE UPPER(DataValue)
        WHEN 'TRUE' THEN 1
        WHEN 'FALSE' THEN 1
        WHEN 'YES' THEN 1
        WHEN 'NO' THEN 1
        WHEN 'Y' THEN 1
        WHEN 'N' THEN 1
        WHEN '0' THEN 1
        WHEN '1' THEN 1
        ELSE 0
      END )
         WHEN '{5951994B-BF47-4117-805D-B8F85FAB76A8}' THEN ISNUMERIC(DataValue)
         ELSE 1
       END AS ValidDataType,
       dbo.tblZEGCode.ZEGCode,
       dbo.qryOtherFieldDataFieldName.FieldName
FROM   dbo.qryOtherFieldDataVerifySource
       LEFT OUTER JOIN dbo.qryOtherFieldDataFieldName
         ON dbo.qryOtherFieldDataVerifySource.ItemID = dbo.qryOtherFieldDataFieldName.ItemID
       LEFT OUTER JOIN dbo.tblBC
                       RIGHT OUTER JOIN dbo.tblZEGCode
                         ON dbo.tblBC.BCID = dbo.tblZEGCode.BCID
         ON dbo.qryOtherFieldDataVerifySource.ZEGCodeID = dbo.tblZEGCode.ZEGCodeID 

有没有人有什么建议?

4

3 回答 3

6

我建议寻找阻止您转换为 float 类型(又名真实)的错误值,并e0在测试之前使用连接到该值的技巧:

SELECT *
FROM dbo.YourTable
WHERE
   DataTypeID = '{5951994B-BF47-4117-805D-B8F85FAB76A8}' -- the type for float
   AND IsNumeric(
      DataValue + CASE WHEN DataValue NOT LIKE '%[ed]%' THEN 'e0' ELSE '' END
   ) = 0
   AND IsNumeric(DataValue) = 1
;

这适用于 SQL Server 2000 及更高版本。

更新1:由于您分享了您只想找到那些不容易检测到的,而不是所有那些不是真正数字的,我添加了第二个IsNumeric

更新2:你终于告诉我你的一些价值观已经有了科学记数法。这很容易处理。我已经更新了上面的查询。请试穿尺寸。

对于使用 SQL Server 2012 或更高版本的任何人,此问题可能最好使用TRY_PARSE解决:

SELECT TRY_PARSE(Value AS float)

这会将任何值转换为可能的浮点数,但会为其他任何值返回 NULL。因此,您可以使用它来检查转换为浮点数是否会失败,方法是检查这个表达式IS NULL

于 2013-06-11T22:03:47.170 回答
0

试试这个——

SELECT
      s.ItemID
    , s.EDGRDataID
    , s.LineItemID
    , s.ZEGCodeID
    , s.DataValue
    , t.AcceptableValues
    , s.DataUnitID
    , s.DataDate
    , t.DataTypeID
    , ValidPositiveNumericValue =
        CASE WHEN DataTypeID = '{5951994B-BF47-4117-805D-B8F85FAB76A8}' AND ISNUMERIC(DataValue) = 1 
            THEN CASE WHEN CAST(DataValue AS FLOAT(8)) >= 0 THEN 1 ELSE 0 END
            ELSE 0
        END
    , ValidDataType =
       CASE 
        WHEN DataTypeID = '{A6317BA5-F8FB-4866-A26B-24594650C2DC}'
            THEN CASE WHEN UPPER(DataValue) IN ('TRUE', 'FALSE', 'YES', 'NO', 'Y', 'N', '0', '1') THEN 1 ELSE 0 END 
        WHEN DataTypeID = '{5951994B-BF47-4117-805D-B8F85FAB76A8}' 
            THEN ISNUMERIC(DataValue)
         ELSE 1
       END
    , c.ZEGCode
    , f.FieldName
FROM dbo.qryOtherFieldDataVerifySource s
LEFT JOIN dbo.qryOtherFieldDataFieldName f ON s.ItemID = f.ItemID
RIGHT JOIN dbo.tblZEGCode c ON s.ZEGCodeID = c.ZEGCodeID
LEFT JOIN dbo.tblBC t ON t.BCID = c.BCID 
于 2013-06-12T06:00:05.107 回答
0

创建了以下游标以使查询一次执行一行,这使我能够识别问题数据行:

SET ARITHABORT OFF
SET ARITHIGNORE ON
SET ANSI_WARNINGS OFF

DECLARE @msg VARCHAR(4096)

BEGIN TRY

    DECLARE @itemid AS NVARCHAR(255);

DECLARE C CURSOR FAST_FORWARD FOR
    SELECT ItemID AS itemid
    FROM dbo.qryOtherFieldDataVerifySource;

OPEN C;

FETCH NEXT FROM C INTO @itemid;

WHILE @@fetch_status = 0
BEGIN

    SELECT  dbo.qryOtherFieldDataVerifySource.ItemID, dbo.qryOtherFieldDataVerifySource.EDGRDataID, dbo.qryOtherFieldDataVerifySource.LineItemID, 
            dbo.qryOtherFieldDataVerifySource.ZEGCodeID, dbo.qryOtherFieldDataVerifySource.DataValue, dbo.tblBC.AcceptableValues, 
            dbo.qryOtherFieldDataVerifySource.DataUnitID, dbo.qryOtherFieldDataVerifySource.DataDate, dbo.tblBC.DataTypeID, 
            CASE WHEN DataTypeID = '{5951994B-BF47-4117-805D-B8F85FAB76A8}' AND ISNUMERIC(DataValue) = 1 THEN (CASE WHEN CAST(DataValue AS Float(8)) 
            >= 0 THEN 1 ELSE 0 END) ELSE 0 END AS ValidPositiveNumericValue, 
            CASE DataTypeID WHEN '{A6317BA5-F8FB-4866-A26B-24594650C2DC}' THEN (CASE UPPER(DataValue) 
            WHEN 'TRUE' THEN 1 WHEN 'FALSE' THEN 1 WHEN 'YES' THEN 1 WHEN 'NO' THEN 1 WHEN 'Y' THEN 1 WHEN 'N' THEN 1 WHEN '0' THEN 1 WHEN '1' THEN 1 ELSE
            0 END) WHEN '{5951994B-BF47-4117-805D-B8F85FAB76A8}' THEN ISNUMERIC(DataValue) ELSE 1 END AS ValidDataType, dbo.tblZEGCode.ZEGCode, 
            dbo.qryOtherFieldDataFieldName.FieldName
     FROM   dbo.qryOtherFieldDataVerifySource LEFT OUTER JOIN
            dbo.qryOtherFieldDataFieldName ON dbo.qryOtherFieldDataVerifySource.ItemID = dbo.qryOtherFieldDataFieldName.ItemID LEFT OUTER JOIN
            dbo.tblBC RIGHT OUTER JOIN
            dbo.tblZEGCode ON dbo.tblBC.BCID = dbo.tblZEGCode.BCID ON dbo.qryOtherFieldDataVerifySource.ZEGCodeID = dbo.tblZEGCode.ZEGCodeID
     WHERE dbo.qryOtherFieldDataVerifySource.ItemID = '' + @itemid + ''

    FETCH NEXT FROM C INTO @itemid;
END

CLOSE C;

DEALLOCATE C;

END TRY 
BEGIN CATCH
SET @msg = 'Failure occurred attempting to execute qryOtherFieldDataVerify.';
EXEC ApplicationManagement.dbo.spLogProcedureCall 
    @ObjectID       = @@PROCID,
    @AdditionalInfo = @msg;
RAISERROR(@msg, 0, 1);
END CATCH   

DECLARE @returnCode int
SET @returnCode = CASE WHEN @msg IS NOT NULL THEN 1 ELSE 0 END
SELECT @returnCode AS ErrCode;
于 2013-06-11T22:03:40.807 回答