简体   繁体   English

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

[英]SQL - Error converting data type varchar to numeric

I have data in a table that has decimals and stored as a varchar type data. 我在具有小数的表中有数据并存储为varchar类型数据。 I need to place that data into another table with column that is data type of decimal(18,5) 我需要将该数据放入另一个表中,该表的列为十进制数据类型(18,5)

I get error: 我收到错误:

 Error converting data type varchar to numeric

The problem is with this Share column 问题在于此Share

Query I have is 查询我有

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

So if I try to only use IFS.Shares that doesn't work, I was trying some other SUM and CAST , but the last thing is that I'm trying to do is that Shares = Convert ..... 因此,如果我尝试仅使用IFS.Shares ,我正在尝试其他一些SUM和CAST,但最后一件事是我要做的就是Shares = Convert .....

Should not be too relevant to this, but I do want to do an insert into and so I do have an insert statement right above that Select statement INSERT INTO [InterclassExchangeBatchDetails](Bin,FromCusip,FromSymbol,Shares) 不应该与此太相关,但我确实想要插入,所以我确实有一个插入语句正上面的Select语句INSERT INTO [InterclassExchangeBatchDetails](Bin,FromCusip,FromSymbol,Shares)

The data that I'm working with for reference that is causing the problem, here is a sample of it 我正在使用的数据是引起问题的参考,这里是它的一个示例

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

只需使用TRY_CONVERT()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM