你可以做
CAST(CAST(value AS float) AS decimal(36, 20))
但...
如果你这样做,cast-to-float 也将改变所有没有 E 内部的值 - 因为浮点数只能与 machine-epsilon 一样精确......
您可以通过仅在数字包含 E (+/-) 时强制转换为浮点数来缓解这种情况。
如果是普通十进制数,直接转换为十进制即可。
这将最大限度地减少浮点舍入误差。
SELECT
CASE
WHEN factor LIKE '%E-%' THEN CAST(CAST(factor AS FLOAT) AS DECIMAL(36, 20))
WHEN factor LIKE '%E+%' THEN CAST(CAST(factor AS FLOAT) AS DECIMAL(36, 20))
ELSE CAST(factor AS DECIMAL(36, 20))
END AS factor
FROM T_IFC_Import_Currency
或更紧凑:
SELECT
CASE
WHEN factor LIKE '%E[+-]%' THEN CAST(CAST(factor AS FLOAT) AS DECIMAL(36, 20))
ELSE CAST(factor AS DECIMAL(36, 20))
END AS factor
FROM T_IFC_Import_Currency
注意:
不要将其简化为:
SELECT
CAST
(
CASE
WHEN factor LIKE '%E[+-]%' THEN CAST(factor AS FLOAT)
ELSE factor
END
AS decimal(36,20)
) AS factor
FROM T_IFC_Import_Currency
因为这意味着 case 表达式在被转换为十进制之前首先被隐式转换为浮点数......