这是T-SQL的一部分。我遇到了以下错误。任何人都可以指导我。问题是因为值列是 nvarchar 数据类型
SELECT RuleID, SourceID, DataFileID, ChildCount, DP_State
FROM
(SELECT DP_State.RuleID, CAST(DP_State.SourceID AS VARCHAR(20)) AS SourceID, CAST(DP_State.DataFileID AS VARCHAR(20)) AS DataFileID, ChildCount, DP_State
FROM (
SELECT RuleID ,
RuleResultID ,
CASE WHEN ISNUMERIC(ISNULL([ResultValue], 0)) = 1 THEN
CAST(ISNULL([Value], 0) AS BIGINT)
ELSE
-1
END AS ChildCount,
我收到错误:
答案 0 :(得分:2)
如果您使用的是SQL Server 2012或更高版本,请尝试此操作
SELECT RuleID,
SourceID,
DataFileID,
ChildCount,
DP_State
FROM (
SELECT DP_State.RuleID,
CAST(DP_State.SourceID AS VARCHAR(20)) AS SourceID,
CAST(DP_State.DataFileID AS VARCHAR(20)) AS DataFileID,
ChildCount,
DP_State
FROM (
SELECT RuleID,
RuleResultID,
CASE
WHEN TRY_CONVERT(INT, ISNULL([ResultValue],0)) IS NOT NULL
THEN CAST(ISNULL([Value], 0) AS BIGINT)
ELSE - 1
END AS ChildCount,
)
)
答案 1 :(得分:1)
您首先在[value]
中对0
进行ISNULL()
检查,然后隐式转换为整数。只有在那之后你才会转换为整数,而是尝试以下方法:
对字符串进行评估:
CAST(ISNULL([Value], '0') AS BIGINT)
或首先转换为整数:
ISNULL(CAST([Value] AS BIGINT), 0)