简体   繁体   中英

SQL - Error converting data type varchar to numeric

I have data in a table that has decimals and stored as a varchar type data. I need to place that data into another table with column that is data type of decimal(18,5)

I get error:

 Error converting data type varchar to numeric

The problem is with this Share column

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 .....

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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