执行时出现错误
我在删除硬编码值时遇到错误。
我需要编写简单的 select 语句,而不是这个 case 语句...使用临时表...
select case [BVG]
when 1 then 1
when 2 then 2
when 3 then 3
end as Q0,
SELECT CASE [AVG]
when 1 then 1
when 1.33 then 2
when 1.5 then 2
when 1.67 then 3
when 2 then 3
when 2.33 then 4
End as Q
FROM [MS].[BE].[Survey]
所以我使用临时表编写了代码...
SELECT [Source], [Score]
INTO #Temp_Table
FROM [MS].[dbo].[S_Survey]
WHERE [data_Source] = 'USA'
而不是那个 case 语句,我替换了这个 select 语句...
SELECT q.[Score] as Q --- Getting error in this place.Data Type is varchar (100).
FROM [MS].[BE].[Survey] s
LEFT OUTER JOIN #Temp_Table q on
s.[AVG] = q.[Source]
但我在执行时遇到错误....错误是
消息 8114,级别 16,状态 5,第 1 行 将数据类型 varchar 转换为 float 时出错。
而不是 q.[score] 附近的那个地方作为 Q
我可以写什么......以及如何编写语法......
谢谢, 萨哈斯拉
I am getting an error while eleminating the hard coded values.
Instead of this case statement I need to write simple select statement...using temp tables...
select case [BVG]
when 1 then 1
when 2 then 2
when 3 then 3
end as Q0,
SELECT CASE [AVG]
when 1 then 1
when 1.33 then 2
when 1.5 then 2
when 1.67 then 3
when 2 then 3
when 2.33 then 4
End as Q
FROM [MS].[BE].[Survey]
So I have written a code using temp table.....
SELECT [Source], [Score]
INTO #Temp_Table
FROM [MS].[dbo].[S_Survey]
WHERE [data_Source] = 'USA'
Instead of that case statement I am replacing this select statement....
SELECT q.[Score] as Q --- Getting error in this place.Data Type is varchar (100).
FROM [MS].[BE].[Survey] s
LEFT OUTER JOIN #Temp_Table q on
s.[AVG] = q.[Source]
But I am getting an error while executing.... And the error is
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
Instead of that place near q.[score] as Q
what can I write.... and how can I write the syntax...
Thanks,
Sahsra
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能平均 varchar。
试试这个
You can't average varchars.
Try this
我猜测 s.AVG 是浮点数,而 q.Source 不是。这将导致 SQL Server 对 JOIN
ON s.[AVG] = q.[Source]
执行隐式转换,并且您的值之一不是字符。您应该再看一下我的答案 或Martin 上次您遇到此问题时。
例如
更新如果您需要删除四个字符值“NULL”,您可能可以执行以下操作(尚未测试),
或者您无法将它们插入到#Temp_Table 中。
或者您可以在尝试加入之前删除这些值。
当然,这让我想为什么你不在 #temp_table 上使用数字类型呢?
I'm guessing that
s.AVG
is a float andq.Source
isn't. This would cause SQL Server to do an implicit conversion on the JOINON s.[AVG] = q.[Source]
and one of your values isn't a character. You should take another look at either my answer orMartin's the last time you had this problem.
e.g.
UPDATE If you need to remove the four character value 'NULL' you could probably do the following (haven't tested)
Or you could not insert them into the #Temp_Table to begin with.
Or you could delete those values before you try and JOIN.
Of course this makes me think why aren't you using a Numerical type on the #temp_table to begin with?