Numeric field overflow exception - sqldatatypes

How I should rewrite my insert statement ?
CREATE TABLE test_table (
rate decimal(16,8)
);
INSERT INTO test_table VALUES (round(3884.90000000 / 0.00003696, 8));
Exception:
ERROR: numeric field overflow
SQL state: 22003
Detail: A field with precision 16, scale 8 must round to an absolute value less than 10^8. Rounded overflowing value: 105110930.73593074
Database: Greenplum Database 4.3.8.0 build 1 (based on PostgreSQL 8.2.15)

You should use decimal(17,8)
CREATE TABLE test_table
(
rate decimal(17,8)
);
Use decimal in below format
decimal(precision, scale)
1) The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point
2) The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point
Since the result of your insert statement is 105110930.73593074, Total number of digits is 17 and after decimal it has 8 so you should use decimal(17,8)
Select (round(3884.90000000 / 0.00003696, 8));

Related

I want my data upto 6 decimal places in impala

I have a double type column in impala
while I am trying to cut it upto some decimal places
I got this error
ERROR: AnalysisException: No matching function with signature: truncate(DOUBLE, TINYINT).
e.g select truncate(cast(0.4893617021276596 as double),7);
any workaround will be welcome
You can use round():
select round(col, 6)
If you actually want a truncate, then subtract 0.0000005:
select round(col - 0.0000005, 6)
Using the DECIMAL type, it is possible to represent numbers with greater precision than the FLOAT or DOUBLE types can represent.
The maximum allowed precision and scale of the DECIMAL type are both 38.
Precision is the total number of digits, regardless of the location of the decimal point.
Scale is the number of digits after the decimal place.
To represent the number 8.54 without a loss of precision, you would need a
DECIMAL type with precision of at least 3, and scale of at least 2.
Example:
Note that the DECIMAL(17,16) type means there is a total of 17 digits, with 16 of them after the decimal point.
DECIMAL(17,16) 3.1415926535897932
You could ALTER your table with DECIMAL type as follow:
ALTER TABLE my_table CHANGE field field DECIMAL(precision, scale);
or as suggest #Gordon Linoff, you could use round() function.

Arithmetic overflow error on decimal field

I have a field cost with values 0.987878656435798654 , 0.765656787898767
I am trying to figure out what would be the datatype for this.
When I give decimal 15,15 and trying to load data it is throwing me an error
Arithmetic overflow error converting varchar to data type numeric.
The problem is that you are not allocating any length to the value before the decimal.
DECIMAL (15, 15) means that it has a precision of 15 digits after the decimal, but only enough room for 15 digits total - thus leaving no room for values greater than 1.
This means that DECIMAL (15, 15) only supports values in the following range:
-0.999999999999999 to 0.999999999999999 (15 digits after the decimal).
You have 18 digits in your first example, so I would recommend using something like DECIMAL (21, 18)
DECIMAL (21, 18) will support values in the range from: -999.999999999999999999 to 999.999999999999999999 (18 digits after the decimal).
But, you should analyze your own data to see what the maximum value would be that you need to support.
Try this...
SELECT LEN(YourColumn)
FROM YourTable
Then , if they are below 1 every time, try this...
SELECT CONVERT(DECIMAL(X,X-1),YourColumn)
Where X is what is returned in the LEN statement. and X-1 is one less than that.
Remember, it's DECIMAL(TotalLength,Precision) so you need to make sure have enough space for the total value.

Arithmetic overflow error converting numeric to data type numeric

I am facing an error on my SQL script:
Arithmetic overflow error converting numeric to data type numeric
select x.MemberName,x.DOB,x.FilePath,x.Medication,x.NDC,x.Directions,x.Name,x.Strength,x.GenericName,x.QtyOrdered,x.DaysSupply,x.DateFilled,
CASE
WHEN x.test = 0 THEN 'N/A'
WHEN compliance > 100.0 THEN '100.0'
ELSE CONVERT(VARCHAR(5), CAST(FLOOR(compliance *10)/10.0 AS DECIMAL(3,1)))
END as [Compliance]
I am facing the error on just above syntax line.
Here's your problem:
declare #compliance decimal(10,5)
set #compliance = 100.0 -- <----------------
select CAST(FLOOR(#compliance *10)/10.0 AS DECIMAL(3,1))
Throws "Arithmetic overflow error converting numeric to data type numeric" error. Changing to DECIMAL(4,1) works, or as #paola suggests, change your condition to >= 100.0
decimal(p,s):
p (precision) is the maximum total number of decimal digits that will
be stored, both to the left and to the right of the decimal point. The
precision must be a value from 1 through the maximum precision of 38.
The default precision is 18.
s (scale) is the number of decimal digits that will be stored to the
right of the decimal point. This number is subtracted from p to
determine the maximum number of digits to the left of the decimal
point.
In your case decimal(3, 1) means a total of 3 digits with 1 digit to the right of the decimal point,
99.9
whereas decimal(4,1) provides a total of 4 digits with 1 digit to the right of the decimal point,
999.9
This questions has already been answered, but the why is important.
Numeric defines the TOTAL number of digits, and then the number after the decimal.
So DECIMAL(4,1) shows 123.4
DECIMAL(4,3) shows 1.234
In both cases you have a total of 4 digits. In one case you have 1 after the decimal, leaving 3 in front of the decimal. And vice versa.

Converting float to decimal in SQL Server 2008

I have a view which needs to return type decimal for columns stored as float.
I can cast each column to decimal as follows:
, CAST(Field1 as decimal) Field1
The problem with this approach, is that decimal defaults to 18,0, which automatically rounds the float columns to 0. I would like to keep a precision of up to 12 decimal places.
However, if I do this:
, CAST(Field1 as decimal(12,12)) Field1
I get a runtime error:
"Arithmetic overflow error converting float to data type numeric"
the float column is defined as length: 8 Precision: 53 in the table. I can not modify anything about the table.
What's the proper way to cast it as decimal w/out losing decimal precision?
12, 12 means no digits before the decimal separator: 12 digits in total, 12 of them being after the period.
Use a more appropriate range, say:
DECLARE #var FLOAT = 100
SELECT CAST(#var as decimal(20,12))
which gives you 8 digits before the separator, or adjust it as needed.

Converting column decimal(18,8) to another column decimal(18,18)

I have a column which has decimal value of 18,8. I was asked to extend it to 18,18 to hold more places after ,.
ALTER TABLE [dbo].[TransakcjeGotowkowe]
ALTER COLUMN TransakcjeGotowkoweKwota decimal (18,18) NULL
Msg 8115, Level 16, State 8, Line 1
Arithmetic overflow error converting numeric to data type numeric.
The statement has been terminated.
I have also tried to do it by GUI. Nothing else changes just want to hold more data after ,.
Is there other way to do this ?
The Decimal datatype is made up of (precision, scale)
Precision is the total number of digits to the left AND the right of the decimal point.
Scale is the number of digits to the right of the decimal point.
If you want to increase the number of digits to the right to 18 you will need to increase the overall precision. In your case increase it by 10.
So you will need decimal(28,18)
MSDN Article on Precision & Scale
You would need to change it to 28,18 Your current column allows 10 digits to the left of the decimal point.
Changing it to 18,18 would only allow a range between +/-0.999999999999999999