转换数据类型时出错。 CONVERT(钱,char(10)' good')?

时间:2015-04-16 11:40:31

标签: sql-server types

我试图标记ListPrice>然后是好的'。

SELECT          
bom.[ProductAssemblyID]             
,bom.[ComponentID]
,p.[Name]
,p.[ListPrice]

FROM        
[Production].[BillOfMaterials] bom      
right join [Production].[Product] p         
on bom.ProductAssemblyID = p.ProductID

Where 
[ProductAssemblyID] is not null 
and [ComponentID] is not null
and [ListPrice] = (case when [ListPrice]> 100 then (convert(money,char(10),'good')) end )

我收到此错误,但我的代码与varchar()

无关
  

消息8116,级别16,状态1,行16参数数据类型varchar是   对转换函数的参数3无效。

2 个答案:

答案 0 :(得分:2)

我想你想要制作你的标志" SELECT中的一列,而不是WHERE子句的一部分,如下所示:

SELECT          
    bom.[ProductAssemblyID]             
    ,bom.[ComponentID]
    ,p.[Name]
    ,p.[ListPrice]
    , CASE
        WHEN p.ListPrice > 100 THEN 'GOOD'
        ELSE ''
    END AS 'flag'

FROM [Production].[BillOfMaterials] bom         
RIGHT JOIN [Production].[Product] p         
ON bom.ProductAssemblyID = p.ProductID

WHERE [ProductAssemblyID] is not null 
AND [ComponentID] is not null

答案 1 :(得分:1)

首先阅读CAST and CONVERT

(convert(money,char(10),'good')) is invalid.

因为您正在尝试convert'good'(a string value)到moneyacceptable decimal/integer value

所以,你需要尝试:

  SELECT          
    a.[ProductAssemblyID]             
    ,a.[ComponentID]
    ,b.[Name]
    ,b.[ListPrice]
    , CASE
        WHEN b.ListPrice > 100 THEN 'higher than 100'
        ELSE 'less than 100'
    END AS result

FROM [Production].[BillOfMaterials] a
RIGHT JOIN [Production].[Product] b         
ON a.ProductAssemblyID = b.ProductID

WHERE a.[ProductAssemblyID] is not null 
AND a.[ComponentID] is not null