我有以下代码来计算重量,但是当我运行它时我得到了
算术溢出错误转换表达式数据类型nvarchar
UPDATE T1
SET [1 WO # WGHT] = CASE
WHEN t2.[WEIGHT] = 'NMI' THEN 0
WHEN t2.[WEIGHT] = '' THEN 0
WHEN t2.[WEIGHT] IS NULL THEN 0
ELSE CAST(t2.[WEIGHT] AS float)
END
FROM [PACKING SLIPS] T1
INNER JOIN inserted i ON T1.[AUTO PACK SLIP #] = i.[AUTO PACK SLIP #]
CROSS APPLY
(SELECT TOP 1 *
FROM [WORKORDERS] T2
WHERE T1.[1. WO #] = T2.[WORK ORDER NO.]) T2
答案 0 :(得分:0)
将CASE表达式替换为以下内容......
ISNULL(TRY_CAST(t2.WEIGHT AS FLOAT), 0)
如果您使用的是旧版本的SQL Server(2008R2及更早版本),以下内容也可以使用...
CAST(CASE WHEN t2.WEIGHT NOT LIKE '%[^0-9.]%' THEN t2.WEIGHT ELSE '0' END AS FLOAT)
旁注...您不应该将FLOAT或REAL数据类型用于货币值。如果您不想使用MOMEY或SMALLMONEY,请考虑使用DECIMAL或NUMERIC。