Getting an error when casting float to decimal - sql

Moving data from flost to decimal(5,2). largest value I could find int he existing data is 94.23 but when I try to cast to decimal it throws error.
Arithmetic overflow error converting float to data type numeric.
I tried copying straight over without casting, got that error. So then I tried casting first:
CAST(Purity as decimal(5,2))
Same error.
I noticed there are also nulls in there so I tried:
ISNULL(CAST(Purity as decimal(5,2)),0)
Same error.

Did you look for the largest value in the database?
select max(Purity)
from t;
And, in the event that Purity is really a string, you might have other things going on. So, you can try:
select max(convert(purity, 18, 6)) -- or something like this
from t;

Related

SQL Code Error converting data type varchar to float

The following code encounters an error when executed in Microsoft Server Management Studion:
USE [DST]
GO
Select
CAST([Balance] as float)
FROM [RAW_XXX]
WHERE ISNUMERIC(Balance) = 1
Msg 8114, Level 16, State 5, Line 2
Error converting data type varchar to float.
I thought that the ISNUMERIC would exclude anything that can not be cast or converted.
It is a massive database in SQLServer 2012 so I am unsure how to find the data that is causing the error.
Use TRY_CONVERT to flush out the offending records:
SELECT *
FROM [RAW_XXX]
WHERE TRY_CONVERT(FLOAT, Balance) IS NULL;
The issue with your current logic is that something like $123.45 would be true according to ISNUMERIC, but would fail when trying to cast as floating point.
By the way, if you wanted a more bare bones way of finding records not castable to float you could just rely on LIKE:
SELECT *
FROM [RAW_XXX]
WHERE Balance NOT LIKE '%[^0-9.]%' AND Balance NOT LIKE '%.%.%';
The first LIKE condition ensures that Balance consists only of numbers and decimal points, and the second condition ensures that at most one decimal point appears. Checkout the demo below to see this working.
Demo

SQL: Arithmetic overflow error converting numeric data type to numeric

I am trying to create the following view:
CREATE VIEW [dbo].[_TEST]
AS
SELECT
a.[PS],
b.[A0001],
CAST(a.[Area] AS DECIMAL(15, 10)) AS A0002,
CAST((b.[0001] / a.[Area]) AS DECIMAL(15, 10)) AS A0003
FROM
dbo.Areas AS a
INNER JOIN
(SELECT
LEFT(PSOA, 6) AS PS, SUM([0001]) as A0001
FROM
dbo.Counts
GROUP BY
LEFT(PSOA, 6) AS b ON a.PS = b.PS
The view creates fine but I get the error below when using it:
Arithmetic overflow error converting numeric data type to numeric
Now, from my understanding, that means the values are too big to fit into a Decimal(15,10). However, manually calculating them, the biggest value I get for A0003 is 2151.93382, which is fine for a Decimal(15,10). I even tried Decimal(20,10) just to be sure and that returns the same error. The figures are definitely not too big, so what's going wrong?
I found that the max Area value was actually a Decimal(16,10) which explains this. Not sure how I missed that!
This is not giving the error and that error might be due to some other reason. Check with your other data which can be bigger than this.
Select Cast(2151.93382 as Decimal(15,10))
You can check the demo here.
How many rows are there in the table I think SUM([0001]) can produce a big number in resulting arithmetic overflow exception.

Arithmetic overflow error converting varchar to data type numeric?

Just now I was getting this error when running a stored procedure:
Arithmetic overflow error converting varchar to data type numeric.
I located the line where that error was coming from and this is the code on that line:
SELECT #AF_MIN_3L = LEFT(MIN([A-F Est_CY]), 6) - 0.000001 FROM #Ent_AF_3
Earlier in the stored procedure, I declared #AF_MIN_3L as data type FLOAT, created the temp table #Ent_AF_3 and in doing so, made the column [A-F Est_CY] data type FLOAT. Is the following code creating a non-FLOAT value?
LEFT(MIN([A-F Est_CY]), 6) - 0.000001
I hope it's a simple casting issue and all I have to do is something like this:
LEFT(MIN(CAST([A-F Est_CY] AS FLOAT)), 6) - CAST(0.000001 AS FLOAT)
I didn't want to run the whole procedure again without being sure I fixed the issue. Thanks for any help.
If you use a string function, which is what LEFT is, it resturns a string value. As described here, the return datatype of the LEFT function does indeed return a VARCHAR or NVARCHAR. So, ideed, a CAST or CONVERT back to FLOAT is required. You should of course convert back to FLOAT AFTER the LEFT function, so it would be: CAST(LEFT(...) as FLOAT).
The problem is your precision - if you have a float > 0.999999 on the temp file you will get the error because of the implicit conversion.
Use:
SELECT CAST(LEFT(MIN([A-F Est_CY]), 6) AS float) - 0.000001 FROM #Ent_AF_4

sql convert error on view tables

SELECT logicalTime, traceValue, unitType, entName
FROM vwSimProjAgentTrace
WHERE valueType = 10
AND agentName ='AtisMesafesi'
AND ( entName = 'Hawk-1')
AND simName IN ('TipSenaryo1_0')
AND logicalTime IN (
SELECT logicalTime
FROM vwSimProjAgentTrace
WHERE valueType = 10 AND agentName ='AtisIrtifasi'
AND ( entName = 'Hawk-1')
AND simName IN ('TipSenaryo1_0')
AND CONVERT(FLOAT , traceValue) > 123
) ORDER BY simName, logicalTime
This is my sql command and table is a view table...
each time i put "convert(float...) part " i get
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.
this error...
One (or more) of the rows has data in the traceValue field that cannot be converted to a float.
Make sure you've used the right combination of dots and commas to signal floating point values, as well as making sure you don't have pure invalid data (text for instance) in that field.
You can try this SQL to find the invalid rows, but there might be cases it won't handle:
SELECT * FROM vwSimProjAgentTrace WHERE NOT ISNUMERIC(traceValue)
You can find the documentation of ISNUMERIC here.
If you look in BoL (books online) at the convert command, you see that a nvarchar conversion to float is an implicit conversion. This means that only "float"-able values can be converted into a float. So, every numeric value (that is within the float range) can be converted. A non-numeric value can not be converted, which is quite logical.
Probably you have some non numeric values in your column. You might see them when you run your query without the convert. Look for something like comma vs dot. In a test scenario a comma instead of a dot gave me some problems.
For an example of isnumeric, look at this sqlfiddle

Error converting data type varchar to float

Searched and searched on SO and can't figure it out
Tried CASTING each field as FLOAT to no avail, convert didn't get me any further
How can I get the below case clause to return the value stated in the THEN section?
Error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
section of my SQL query that makes it error:
When cust_trendd_w_costsv.terms_code like '%[%]%' and (prod.dbo.BTYS2012.average_days_pay) - (substring(cust_trendd_w_costsv.terms_code,3,2)) <= 5 THEN prod.dbo.cust_trendd_w_costsv.terms_code
average_days_pay = float
terms_code = char
Cheers!
Try to use ISNUMERIC to handle strings which can't be converted:
When cust_trendd_w_costsv.terms_code like '%[%]%'
and (prod.dbo.BTYS2012.average_days_pay) -
(case when isnumeric(substring(cust_trendd_w_costsv.terms_code,3,2))=1
then cast(substring(cust_trendd_w_costsv.terms_code,3,2) as float)
else 0
end)
<= 5 THEN prod.dbo.cust_trendd_w_costsv.terms_code
The issue that you're having is that you're specifically searching for strings that contain a % character, and then converting them (implicitly or explicitly) to float.
But strings containing % signs can't be converted to float whilst they still have a % in them. This also produces an error:
select CONVERT(float,'12.5%')
If you're wanting to convert to float, you'll need to remove the % sign first, something like:
CONVERT(float,REPLACE(terms_code,'%',''))
will just eliminate it. I'm not sure if there are any other characters in your terms_code column that may also trip it up.
You also need to be aware that SQL Server can quite aggressively re-order operations and so may attempt the above conversion on other strings in terms_code, even those not containing %. If that's the source of your error, then you need to prevent this aggressive re-ordering. Provided there are no aggregates involved, a CASE expression can usually avoid the worst of the issues - make sure that all strings that you don't want to deal with are eliminated by earlier WHEN clauses before you attempt your conversion
If your are sure that Substring Part returns a numeric value, You can Cast The substring(....) to Float :
.....and (prod.dbo.BTYS2012.average_days_pay) - (CAST(substring(cust_trendd_w_costsv.terms_code,3,2)) as float ) <= 5 ....