我对 SQL 很陌生;我正在努力从数据库中提取数据。我正在尝试提取 Au 值 > 1.00 的所有记录。
AttributeValue 列是 NVARCHAR
这是我尝试过的:
SELECT TOP 1000 [Id]
,[DHGeochemistryId]
,[AttributeColumn]
,[AttributeValue]
,[ModifiedDate]
,[ModifiedBy]
,[MRTDetailId]
FROM [DRILLHOLES_Export].[dbo].[DHGeochemistryAttr]
WHERE [AttributeValue] > 1.00 and [AttributeColumn] = 'Au'
软件抛出以下错误:
Msg 8115, Level 16, State 8, Line 2 Arithmetic overflow error converting nvarchar to data type numeric.
我尝试使用以下方法转换为数字:
SELECT TOP 1000 [Id]
,[DHGeochemistryId]
,[AttributeColumn]
,[AttributeValue]
,[ModifiedDate]
,[ModifiedBy]
,[MRTDetailId]
FROM [DRILLHOLES_Export].[dbo].[DHGeochemistryAttr]
SET myValue = CASE WHEN ISNUMERIC(AttributeValue)=1 THEN CAST(AttributeValue AS float)
ELSE 0 END)
WHERE [AttributeValue] > 1.00 and [AttributeColumn] = 'Au'
软件抛出以下错误:
Msg 102, Level 15, State 1, Line 10 Incorrect syntax near '='. (Line 10 is the “SET” command)
我正在使用 SQL Server 2014,希望得到一点帮助。
最佳答案
第一个语句提示,因为 AttributeValue
不是数字列。不过你可以CAST
它:
WHERE CAST([AttributeValue] AS FLOAT) > 1.00
但是如果其中的任何值不是数字,则可能会出错。因此,您可以在转换之前使用 ISNUMERIC
:
WHERE ISNUMERIC([AttributeValue]) = 1
AND CAST([AttributeValue] AS FLOAT) > 1.00
SQL Server 足够聪明,能够短路逻辑,因此如果 WHERE
子句的第一部分返回 false,它就不会尝试 CAST
。
关于sql - 从数据库中提取值 > 1.00,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33502040/