SQL 中的数据转换错误,不确定它可能指的是哪个字段
Data Conversion error in SQL, unsure what field it could be referring to
消息 8114,级别 16,状态 5,第 1 行将数据类型 varchar 转换为数字时出错。
下面是我正在使用的代码以及代码中提到的字段的架构。这背后的一些背景,我有一组 indcodes 列表。我只在下面给出一个,但总共有 350 个。当我通过 indcode、区域和所有权查询数据不存在时,很可能。因此,为什么我有 isnull 语言。最后,我需要包含值 suppress = 1 的行显示为 N/A。
对于我查询的每个 indcode,我都需要一个结果。结果可能是实际数据或一些文本(NULL、N/A 等)。
最后是预期的结果。我只给出了一个 indcode 的代码,但包含了六个以让示例更深入。
select
CASE
WHEN suppress = 0 then isnull (mnth1emp, 0)
ELSE 'N/A'
END as mnth1emp,
CASE
WHEN suppress = 0 THEN isnull (mnth2emp, 0)
ELSE 'N/A'
END as mnth2emp,
CASE
WHEN suppress = 0 THEN isnull (mnth3emp, 0)
ELSE 'N/A'
END as mnth3emp
from dbo.industryimport20172f
where area='000003' and indcode='21' and ownership='00'
suppress char (1)
mnth1emp numeric(9,0)
mnth2emp numeric(9,0)
mnth3emp numeric(9,0)
area char(6)
indcode char (6)
ownership char(2)
123 456 789
1 2 3
Null null null
2 3 4
3 4 5
Null Null Null
所以这里跳出的第一件事是:
a) 当您要求在 case 语句中返回数字时,您在到达 ELSE 时要求返回一个字符串。尝试将所有字段转换为 varchars,然后将它们作为字符串输入:
CASE
WHEN cast(suppress as varchar) = '0' then isnull (cast(mnth1emp as varchar), '0')
ELSE 'N/A'
END as mnth1e
你可能不需要施展压制。
b) supress 中是否有除数字以外的任何内容?如果是这样,您需要将零作为字符串:
CASE WHEN supress = '0' --etc.
消息 8114,级别 16,状态 5,第 1 行将数据类型 varchar 转换为数字时出错。
下面是我正在使用的代码以及代码中提到的字段的架构。这背后的一些背景,我有一组 indcodes 列表。我只在下面给出一个,但总共有 350 个。当我通过 indcode、区域和所有权查询数据不存在时,很可能。因此,为什么我有 isnull 语言。最后,我需要包含值 suppress = 1 的行显示为 N/A。
对于我查询的每个 indcode,我都需要一个结果。结果可能是实际数据或一些文本(NULL、N/A 等)。
最后是预期的结果。我只给出了一个 indcode 的代码,但包含了六个以让示例更深入。
select
CASE
WHEN suppress = 0 then isnull (mnth1emp, 0)
ELSE 'N/A'
END as mnth1emp,
CASE
WHEN suppress = 0 THEN isnull (mnth2emp, 0)
ELSE 'N/A'
END as mnth2emp,
CASE
WHEN suppress = 0 THEN isnull (mnth3emp, 0)
ELSE 'N/A'
END as mnth3emp
from dbo.industryimport20172f
where area='000003' and indcode='21' and ownership='00'
suppress char (1)
mnth1emp numeric(9,0)
mnth2emp numeric(9,0)
mnth3emp numeric(9,0)
area char(6)
indcode char (6)
ownership char(2)
123 456 789
1 2 3
Null null null
2 3 4
3 4 5
Null Null Null
所以这里跳出的第一件事是:
a) 当您要求在 case 语句中返回数字时,您在到达 ELSE 时要求返回一个字符串。尝试将所有字段转换为 varchars,然后将它们作为字符串输入:
CASE
WHEN cast(suppress as varchar) = '0' then isnull (cast(mnth1emp as varchar), '0')
ELSE 'N/A'
END as mnth1e
你可能不需要施展压制。
b) supress 中是否有除数字以外的任何内容?如果是这样,您需要将零作为字符串:
CASE WHEN supress = '0' --etc.