执行时出现错误

发布于 2024-10-10 11:06:17 字数 964 浏览 0 评论 0原文

我在删除硬编码值时遇到错误。

我需要编写简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-10-17 11:06:17

你不能平均 varchar。

试试这个

SELECT [Source] , cast( [Score] as decimal (10,2) )--or whatever you need it to be

INTO #Temp_Table

FROM [MS].[dbo].[S_Survey]

WHERE [data_Source] = 'USA'

You can't average varchars.

Try this

SELECT [Source] , cast( [Score] as decimal (10,2) )--or whatever you need it to be

INTO #Temp_Table

FROM [MS].[dbo].[S_Survey]

WHERE [data_Source] = 'USA'
寄居人 2024-10-17 11:06:17

我猜测 s.AVG 是浮点数,而 q.Source 不是。这将导致 SQL Server 对 JOIN ON s.[AVG] = q.[Source] 执行隐式转换,并且您的值之一不是字符。您应该再看一下我的答案
Martin 上次您遇到此问题时。

例如

SELECT  q.[Score] 
FROM [MS].[BE].[Survey] s
WHERE like Score '%[^0-9.]%'

更新如果您需要删除四个字符值“NULL”,您可能可以执行以下操作(尚未测试),

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]
and q.[Source] <> 'NULL'

或者您无法将它们插入到#Temp_Table 中。

或者您可以在尝试加入之前删除这些值。

当然,这让我想为什么你不在 #temp_table 上使用数字类型呢?

I'm guessing that s.AVG is a float and q.Source isn't. This would cause SQL Server to do an implicit conversion on the JOIN ON s.[AVG] = q.[Source]and one of your values isn't a character. You should take another look at either my answer or
Martin's the last time you had this problem.

e.g.

SELECT  q.[Score] 
FROM [MS].[BE].[Survey] s
WHERE like Score '%[^0-9.]%'

UPDATE If you need to remove the four character value 'NULL' you could probably do the following (haven't tested)

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]
and q.[Source] <> 'NULL'

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文