在 SQL Server 2012 中使用 ISNULL 和 NULLIF 函数时将数据类型 varchar 转换为 float 时出错

Error converting data type varchar to float while using ISNULL and NULLIF function in SQL Server 2012

当我使用以下 SQL 代码时出现此错误:

Error converting data type varchar to float

我的代码:

select 
    [DEVELOPIN], 
    avg([DEVELOPINGCOST]) as [AVG DEVELOPINGCOST],
    avg([SOFTWARECOST]) as [ AVG SOFTWARECOST],
    ISNULL(AVG([DEVELOPINGCOST] / NULLIF([SOLDCOPIES], 0)), 'No Sale') as [AVG PER Sale] 
from
    [dbo].[SOFTWARE] 
group by 
    [DEVELOPIN]

错误已被隔离到这个表达式:

ISNULL(AVG([DEVELOPINGCOST] / NULLIF([SOLDCOPIES], 0)), 'No Sale') as [AVG PER Sale]

我的错误是因为你在 ISNULL 函数中的第一个表达式的结果是 float 并且你正在分配 varchar 类型的值,即 'No Sale' 当 exp 计算为 null 还要确保 nullif 的两个表达式的数据类型功能匹配 尝试将表达式转换为 varchar

select [DEVELOPIN]
      ,avg([DEVELOPINGCOST])as [AVG DEVELOPINGCOST]
      ,avg([SOFTWARECOST]) as [ AVG SOFTWARECOST]
      ,  ISNULL(CAST(AVG([DEVELOPINGCOST]/NULLIF([SOLDCOPIES],0)) as varchar),'No Sale') 
 from[dbo].[SOFTWARE] GROUP BY [DEVELOPIN]