将数据类型varchar转换为float时出错

时间:2011-09-10 15:04:04

标签: sql

我有以下SQL语句,它给了我这个错误。

“在此行中将数据类型varchar转换为float时出错”

   ,  pe.ProductWeight + ' lb' AS weight

我意识到这是错误的,但我不知道如何在重量值中添加“lb”。任何帮助将不胜感激。

SELECT p.ProductCode AS id

,  p.ProductName AS title

,  'Home & Garden > Household Appliance Accessories > Laundry Appliance Accessories' AS product_type

,  IsNull(pe.SalePrice,pe.ProductPrice) AS price

,  IsNull(pe.ProductManufacturer,'n/a') AS brand

,  IsNull(pe.ProductCondition,'new') AS condition

,  CONVERT(VARCHAR(10), (GETDATE() + 30),120) AS expiration_date

,  pd.ProductDescriptionShort AS [stripHTML-description]

,  'http://www.thesite.com/v/vspfiles/photos/' + IsNull(p.Vendor_PartNo,p.ProductCode) + '-2.jpg' AS image_link

,  'http://www.thesite.asp?ProductCode=' + p.ProductCode + '&Click=1327'  AS link

,  pe.ProductWeight + ' lb' AS weight


FROM Products p

INNER JOIN Products_Descriptions pd ON p.ProductID = pd.ProductID

INNER JOIN Products_Extended pe ON pd.ProductID = pe.ProductID

WHERE (p.IsChildOfProductCode is NULL OR p.IsChildOfProductCode = ' AND (p.HideProduct is NULL OR p.HideProduct <> 'Y')

AND (pe.ProductPrice > 0)

ORDER BY p.ProductCode

2 个答案:

答案 0 :(得分:4)

如果要连接float和string,则需要先将float转换为字符串:

   CAST(pe.ProductWeight AS VARCHAR(20)) + ' lb' AS weight

答案 1 :(得分:1)

Varchar的优先级低于float,所以它试图将'lb'变成float并失败。

请参阅http://msdn.microsoft.com/en-us/library/ms190309.aspx了解排名。