首先祝大家新年快乐。我在编写查询时遇到问题。在执行我的查询时,我收到了错误。
查询:
select case
when S.R1 = '6' then 5
when S.R1 = '7' then 6
when S.R1 = '8' then 7
when S.R1 = '9' then 8
when S.R1 ='10' then 9
else S.R1 end as Q
FROM [HelpService].[dbo].[help] s
-----------------------------------------------
SELECT [Source], [Score]
INTO #Temp_Q
FROM [HelpDesk].[dbo].[Survey]
WHERE [data_Source Name] = 'Text Data'
-----------------------------------------------
select CONVERT(REAL, a.[Dell Score]) as Q
FROM [HelpService].[dbo].[help] s
LEFT OUTER JOIN #CE_Temp_Q a on
s.[R1] = a.[Source]
ERROR
Msg 8114,Level 16,State 5,Line 1
将数据类型varchar转换为real。时出错。
我要求做的是我需要删除硬编码值,并需要使用临时表编写查询。
先谢谢, Shashra
答案 0 :(得分:2)
将数据类型varchar转换为实际
时出错
这意味着您的某个值包含的不是数字。
例如,以下工作正常
SELECT convert(Real, '1')
UNION SELECT convert(Real, ' ')
UNION SELECT convert(Real, NULL)
UNION SELECT convert(Real, '123.123')
UNION SELECT convert(Real, ' 456 ')
但是,以下任何一个都会产生相同的错误
SELECT convert(Real, ' 456 ')
SELECT CONVERT(Real, '1 2')
<强>更新强>
有时候问题值不是那么明显
请尝试以下方法找到它
SELECT DISTINCT
a.[Dell Score]
FROM
[HelpService].[dbo].[help] s
LEFT OUTER JOIN #CE_Temp_Q a on
s.[R1] = a.[Source]
OR
SELECT DISTINCT
a.[Dell Score],
DATALENGTH (a.[Dell Score])
FROM
[HelpService].[dbo].[help] s
LEFT OUTER JOIN #CE_Temp_Q a on
s.[R1] = a.[Source]
答案 1 :(得分:1)
以下查询返回什么内容?
select a.[Dell Score]
FROM [HelpService].[dbo].[help] s
LEFT OUTER JOIN #CE_Temp_Q a on
s.[R1] = a.[Source]
WHERE a.[Dell Score] like '%[^0-9.]%'