SQL: Arithmetic overflow error converting numeric data type to numeric - sql

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.

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

Issue converting JSON string into decimal

This question may answer on many threads but I am unable to find answer specific to my problem.
Q: I am getting data from API (in json format) where all columns are coming as string and inserting into a table which has all columns as string and serving as source table.
Now, I am trying to cast data from that source to destination and making all necessary casting to insert data into destination table. But decimal (16,8) casting failed.
I debug issue at my end and found that during the fetching data from API which is returning the data into json format converting values in some unusual format.
For e.g. 0.00007 converting into 7E-05 and this is happening for many other rows.
I know I can fix this problem at API implementation level. But I asked to solve this at SQL server end. So I need a solution which should convert 7E-05 into 0.00007.
Try something like:
SELECT CAST(CAST(#t AS FLOAT) AS DECIMAL(16,8))
Results in:
0.00007000
CAST to a FLOAT, then to a DECIMAL.
This unusual format is a rather usual scientific notation Wikipedia, read section "E-notation"
You see the E and a number meaning exponent.
"1E2" = 1 * 10^2 = 100
"1E-2" = 1 * 10^(-2) = 0.01
Try this out:
DECLARE #tbl TABLE(Numberstring VARCHAR(100));
INSERT INTO #tbl VALUES('100'),('1E2'),('1E-2'),('7E-05');
SELECT Numberstring
,CAST(Numberstring AS FLOAT)
,CAST(CAST(Numberstring AS FLOAT) AS DECIMAL(20,10))
FROM #tbl;
The result
100 100 100.0000000000
1E2 100 100.0000000000
1E-2 0,01 0.0100000000
7E-05 7E-05 0.0000700000
You can see, that the FLOAT type itself will display the last one in the scientific notation, while the cast to DECIMAL will return the number you are expecting.
I'd be happy with an upvote, but you should accept Shawn's answer as it was earlier than mine :-D

Getting an error when casting float to decimal

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;

SQL Server: error converting data type nvarchar to float

I have read many articles and tried several methods but don't have any luck.
I am importing table A (whose cost is char to table B (which requires float).
What I tried:
--cast([Cost] as float)
-- cast(ISNULL([Cost],0) as float)
-- NULLIF(CAST(ISNULL([Cost],0) as float), 0)
convert(float,replace([Cost],',','') )[Cost]
Sample data
FINAL Freight Cost
1248
1248
193.79
201.56
1475.71
97.86
97.86
97.86
125.49
97.86
447.83
450
492.99
450
And I still get this error:
Error converting data type nvarchar to float
Update:
In addition, I am not sure how to modify the existing code based on the answers;
The existing code structure
1. creat table B
2. insert into B () select[DestAddress], [COST] from A
I also tried CAST(test AS FLOAT) AS CastedValue
Why float? The dataset will be sent to an optimization algorithm which requires float. Thanks for pointing it out though.
This works for me?
DECLARE #tbl TABLE(test NVARCHAR(100));
INSERT INTO #tbl VALUES
('1248')
,('1248')
,('193.79')
,('201.56')
,('1475.71')
,('97.86')
,('97.86')
,('97.86')
,('125.49')
,('97.86')
,('447.83')
,('450')
,('492.99')
,('450');
SELECT *
,CAST(test AS FLOAT) AS CastedValue
FROM #tbl;
But the main question is: Why?
Hint 1: Float is the wrong type for this!
From the column name I take, that you are dealing with costs. The FLOAT type is absolutely to be avoided here! You should use DECIMAL or specialised types to cover money or currency values...
Hint 2: NVarchar is the wrong type for this!
And the next question is again: Why? Why are these values stored as NVARCHAR? If ever possible you should solve your problem here...
UPDATE
You edited your question and added this
insert into B () select[DestAddress], [COST]
I do not know the target table's column names, but this should work
INSERT INTO B(ColumnForAddress,ColumnForCost)
SELECT CAST([COST] AS FLOAT),[DestAddress] FROM YourSourceTable
UPDATE 2
After all your comments I'm pretty sure, that there are invalid values within your numbers list. Use ISNUMERIC or - if you are using SQL-Server-2012+ even better TRY_CAST to find invalid values.
Use Convert function , it works smoothly even adding spaces , so there is no need for using Ltrim or Rtrim!
Example:
select convert(float,' 492.99 ') + 1 as converted
Result:
Converted
---------
493.99
FORMAT(ROUND(cast(Total as float), 2), 'C') AS GrandTotal

EXP() in BigQuery returns floating-point error

I have the following query:
SELECT EXP(col) FROM `project.dataset.tablename`;
Where col is FLOAT. However, I get this error: Error: Floating point error in function: EXP.
I've tried EXP() with dummy data, and it works. For example:
SELECT EXP(col) FROM (
SELECT 1. as col UNION ALL
SELECT 2. as col);
Why do I get a floating-point error with actual data and how do I work around it? I've tried such things as EXP(CAST(col as FLOAT64)) and EXP(ROUND(col, n)), but I still get the same error.
Probably you are working with numbers larger than 709.7827.
Weird number, but even in Fortran docs:
EXP(X)
Exponential.
X must be less than or equal to 709.7827.
http://sc.tamu.edu/IBM.Tutorial/docs/Compilers/xlf_8.1/html/lr277.HTM
This because numbers get too large after e^709.7827.
Run the query:
SELECT MAX(col)
FROM project.dataset.tablename;
It will probably then be obvious why you are getting an overflow error. You can work around it by using a case:
SELECT (CASE WHEN col < ?? THEN EXP(col) END)
FROM project.dataset.tablename;
I could suggest a value, but it is probably obvious from your application -- say something larger than 10 or 100 might just be unreasonable.