SQL-将数据类型varchar转换为数值时出错

时间:2018-11-16 21:10:16

标签: sql sql-server tsql

我在具有小数位并存储为varchar类型数据的表中有数据。我需要将该数据放入具有十进制(18,5)数据类型的列的另一个表中

我收到错误消息:

 Error converting data type varchar to numeric

问题出在此Share

查询是

SELECT IFS.Bin,CFL.CUSIP,IFS.[FROM], 
--IFS.Shares 
--SUM(isnull(cast(IFS.Shares as money),0))
--CAST(isnull(IFS.Shares,0) AS VARCHAR(30))
 Shares =
  Convert(
     DECIMAL(18,5), 
     CASE
     WHEN IFS.Shares LIKE '%[^0-9]%' THEN IFS.Shares --NULL
     ELSE IFS.Shares
     END)
FROM 
mfclearing.ICE.ImportFileStaging IFS INNER JOIN 
mfclearing.Production.ClearingFundList CFL ON 
IFS.[From] = CFL.NasdaqSymbol

因此,如果我尝试仅使用不起作用的IFS.Shares,那么我正在尝试其他一些SUM和CAST,但是最后一件事是我试图做的是Shares = Convert ... ..

应该与此无关,但是我确实想在其中插入,因此在该Select语句的上方确实有一个insert语句。 INSERT INTO [InterclassExchangeBatchDetails](Bin,FromCusip,FromSymbol,Shares)

造成问题的我正在参考的数据,这里是一个示例

521.92100000000005
9906.8510000000006
542.529
1043.8409999999999
3129.0839999999998
5285.4120000000003
104.367
126.98
332.02499999999998
530.12300000000005
575.57799999999997
895.56899999999996
1052.9349999999999
1167.0619999999999
1180.9939999999999
1630.8030000000001
247.232
2136.2040000000002
667.95500000000004
947.78599999999994
148.36000000000001
223.994
238.42699999999999
255.25700000000001
257.56999999999999
259.70600000000002
317.90199999999999
317.90199999999999
317.90199999999999
360.59199999999998
366.84399999999999
374.35000000000002
376.90199999999999
393.11500000000001
397.37200000000001
399.47699999999998
449.72699999999998
463.60899999999998
474.68599999999998
488.11599999999999
491.245
504.67399999999998
509.97899999999998
530.47199999999998
535.93299999999999
537.69799999999998
549.40599999999995
552.41700000000003
581.32600000000002
608.05100000000004

1 个答案:

答案 0 :(得分:3)

只需使用TRY_CONVERT()

Shares = TRY_CONVERT(DECIMAL(18,5), IFS.Shares)