简体   繁体   English

将nvarchar转换为int错误

[英]converting nvarchar to int error

I have this query that I can't quite work out the error I'm getting. 我有这个查询,我不能完全解决我得到的错误。

Conversion failed when converting the nvarchar value '1.5' to data type int. 将nvarchar值“ 1.5”转换为数据类型int时,转换失败。

This is due to pvlaue for one case pulling a '1.5' string and trying to convert it to an int implicitly. 这是由于pvlaue在一种情况下拉出“ 1.5”字符串并将其隐式转换为int所致。 I tried to rectify this by casting to a float but this doesn't seem to work for me. 我试图通过将其转换为浮点数来纠正此问题,但这似乎对我不起作用。 Any help in this regard would be very helpful. 在这方面的任何帮助将非常有帮助。

I'm running this on SQL Server 2012 Management Studio. 我正在SQL Server 2012 Management Studio上运行它。

BEGIN DECLARE @Item NVARCHAR(100)
SET @Item = 'Water'

SELECT 
    d.DESCRIPTION, d.ITEM_CODE,  
    el.ENUM_LABEL as Building_Block, 
    CAST(itpC.pvalue AS DECIMAL(22, 3)), ffi.where_used,
    d.STATUS_IND
FROM 
    FSITEM d
LEFT JOIN 
    FSITEMTECHPARAM itp ON d.ITEM_CODE = itp.ITEM_CODE 
                        AND itp.PARAM_CODE = 'BUILDING_BLOCK'
LEFT JOIN 
    FSVALIDENUMVALCF ev ON CAST(coalesce(itp.PVALUE, 0) as float) = CAST(ev.ENUM_VALUE AS FLOAT)
                        AND ev.ENUM_CODE = 'C_BUILDING_BLOCK_TYP'
LEFT JOIN 
    FSVALIDENUMLABELCF el ON ev.ENUM_CODE = el.ENUM_CODE 
                          AND ev.ENUM_ORDER = el.ENUM_ORDER 
                          AND el.LANGUAGE_CODE = 'EN-US'
LEFT JOIN
    (SELECT 
         item_Code, max(CAST(pvalue AS FLOAT)) as pvalue
     FROM 
         FSITEMTECHPARAM
     JOIN
         KC_SITE_COST_MAP ON FSITEMTECHPARAM.PARAM_CODE = 'COST_'+KC_SITE_COST_MAP.COST_CODE
     GROUP BY 
         ITEM_CODE

     UNION

     SELECT
         ITEM_CODE, CAST(pvalue AS FLOAT) as pvalue
     FROM  
         FSFORMULA
     JOIN
         FSFORMULATECHPARAM ON FSFORMULA.FORMULA_ID = FSFORMULATECHPARAM.FORMULA_ID
                            AND PARAM_CODE = 'COST_TOTAL_BASE'
     WHERE 
         FSFORMULA.FORMULA_ID IN (SELECT FORMULA_ID FROM FSITEM)) itpC ON D.item_code = itpC.item_code
LEFT JOIN 
    (SELECT
         ffi.item_code, COUNT(distinct ffi.formula_id) where_used 
     FROM
         fsformulaingred ffi 
     JOIN
         fsformula ff ON ffi.formula_id = ff.formula_id 
     WHERE
         ff.status_ind = 500 AND ff.logical_delete = 0  
     GROUP BY
         ffi.item_code) ffi ON D.item_code = ffi.item_code 
WHERE 
    d.LOGICAL_DELETE = 0 
    AND d.COMPONENT_IND <> 2 
    AND d.Status_IND < 600
    AND (((CAST(ev.ENUM_VALUE as FLOAT) = 3 or CAST(ev.ENUM_VALUE as FLOAT) = 2)  
    AND d.Status_Ind < 600) OR
       CAST(ev.ENUM_VALUE as FLOAT) = 1 or CAST(ev.ENUM_VALUE as FLOAT) = '0') 
    AND (d.Description Like @Item + '%')
    --Or d.Description Like replace(@Item, '!', '%'))
ORDER BY 
    CAST(coalesce(ev.enum_value,0) as FLOAT) DESC, status_ind DESC, d.Item_Code
END

It's the coalesce that is doing the conversion to int , so casting the result from the coalesce is too late. 是将coalesce转换为int ,因此从coalesce转换结果为时已晚。

The result of an expression has to have the same type regardless of how the expression is evaluated. 无论如何评估表达式,表达式的结果必须具有相同的类型。 The expression coalesce(itp.PVALUE, 0) will be of the type int regardless of whether the value itp.PVALUE or the value 0 is used, because the type of 0 is int . 表达coalesce(itp.PVALUE, 0)将成为类型的int无论的值是否itp.PVALUE或者值0时,由于类型0int

If you cast the 0 to float , the type of the expression will be float , so use coalesce(itp.PVALUE, cast(0 as float)) . 如果投射的0float ,表达式的类型将是float ,所以使用coalesce(itp.PVALUE, cast(0 as float))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM