我有这个查询,我无法解决我得到的错误。
将nvarchar值“1.5”转换为数据类型int时,转换失败。
这是由pvlaue
导致一个案例拉出'1.5'字符串并尝试隐式转换为int。我试图通过施放到浮动来纠正这个问题,但这对我来说似乎并不适用。在这方面的任何帮助都会非常有帮助。
我在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
答案 0 :(得分:0)
正在转换为coalesce
的是int
,因此从coalesce
转换结果为时已晚。
无论表达式的计算方式如何,表达式的结果都必须具有相同的类型。无论使用值coalesce(itp.PVALUE, 0)
还是值int
,表达式itp.PVALUE
都属于0
类型,因为0
的类型为{{{ 1}}。
如果您将int
转换为0
,则表达式的类型将为float
,因此请使用float
。