无法将有效的数字varchar转换为十进制

时间:2019-05-23 06:28:49

标签: sql-server

我无法使用CAST或CONVERT将varchar转换为小数。当我检查字符串是否为有效数字时,ISNUMERIC返回为有效数字。但是,当使用CAST或CONVERT时,会抛出“将数据类型varchar转换为数字时出错”。

尝试了不同的选择,但没有运气。如果我从字符串中删除“ e +”,它可以工作,但结果不正确。

我尝试使用REPLACE查看是否有任何东西。但是没用。

select isnumeric( '1.67022e+006') as c,
CAST('1.67022e+006') AS DECIMAL(24, 10)) AS ci,
CAST(replace('1.67022e+006','','') AS DECIMAL(24, 10)) AS cii,
CAST(replace('1.67022e+006','e+','') AS DECIMAL(24, 10)) AS civ,
CAST(1.67022e+006 AS DECIMAL(24, 10)) AS ciii;

CAST('1.67022e+006') AS DECIMAL(24, 10)) --> Error    
CAST(replace('1.67022e+006','','') AS DECIMAL(24, 10)) --> error    
CAST(replace('1.67022e+006','e+','') AS DECIMAL(24, 10)) --> 1.6702200600    
CAST(1.67022e+006 AS DECIMAL(24, 10)) --> 1670220.0000000000

1 个答案:

答案 0 :(得分:0)

我认为您不能直接将CAST的指数数字decimal

CAST()FLOAT,然后到DECIMAL

select  CAST(CAST('1.67022e+006' AS FLOAT) AS DECIMAL(24, 10))