我收到varchar数字错误?

时间:2011-07-27 13:24:46

标签: sql-server

我做错了什么?有没有更好的方法来实现这一点,从字符串中删除长度并将其转换为数值?

UPDATE VF_CasINV_Cost
   SET TotWgt = CASE
        WHEN (Dimension like '%x%') THEN
        (cast(left(Dimension, charindex('X', Dimension, 1) - 1) as numeric))  *
        (cast(substring(Dimension, 1 + charindex('X', Dimension, 1), len(Dimension)) as numeric)) / 144.00

        WHEN TotWgt = 0 and [Wt/Ft]  > 0 THEN  (Dimension * [Wt/Ft]) * [Count]
        WHEN TotWgt = 0 and [Wt/Sht] > 0 THEN ((Dimension * [Wt/Sht]) / 100) * [Count]
        WHEN TotWgt = 0 and [Wt/Pc]  > 0 THEN ((Dimension * [Wt/Pc] ) / 100) * [Count]
        ELSE TotWgt
    END --TotWgt, Item#, TotWgt, [Wt/Ft], [Wt/Sht], [Wt/Pc], [Count], Dimension, TotWgt
FROM VF_CasINV_Cost
WHERE (CalendarYear = 2010) AND (Item# = Item#) AND (ID = ID) AND (Item# < 99999990)
AND K = 'K' AND D = '' AND C = '' AND (Unit = 'cwt' OR Unit = 'cwts')

1 个答案:

答案 0 :(得分:0)

我会做一些假设:Dimension是一个varchar字段;您试图从此varchar字段中解析出数值并在计算中使用它。 如果是这样,那么问题是最后三种情况:您在乘法运算中使用Dimension而不进行强制转换。所以SQL不能在varchar字段上执行乘法。