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.
Related
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.
I'm converting a decimal value with has more than precision which I am converting to
select convert(numeric(8,4),17597.9)
I need to convert this value what is the alternative
The problem is not the precision of the value, but the scale.
The documentation says:
s (scale):
The number of decimal digits that are 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. Scale must be a value from 0 through p, and can only be specified if precision is specified. The default scale is 0 and so 0 <= s <= p. Maximum storage sizes vary, based on the precision.
You've specified a datatype of precision 8, scale 4 - which means that you can have at most 8 digits total, with 4 digits to the right of the decimal point - and by extension of that, a max of 4 digits to the left.
If you want to convert that number, you'll need a to specify a precision p >= s + 5, such as numeric(9,4)
select convert(numeric(9,4),17597.9)
-- 17597.9000
I am trying to round a value in SQL, here is the code that I have:
select round(600.000,2)
How do I get the value 600.00?
Instead of round() convert to a decimal:
select cast(600.000 + 0.5 as decimal(10, 2) )
round() changes the value but it might not change the type of the result. Hence, you might still see extra decimal points (depending on the database and the application). Converting to a decimal with two digits of precision converts both the value and the type.
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.
i am having hard time determining the length of a Decimal data type. The data i have in column is like 0.08,1.2,12.35,121.36. Now if i go for (2,2) it throws an error : Arithmetic overflow error converting numeric to data type numeric. Just wondering should it be (6,2)? and if yes can anybody tell me Why 6 and 2?
In syntax like
NUMERIC(precision, scale)
precision is the total number of digits (count digits on both sides of the decimal point), and scale is the number of digits to the right of the decimal point.
From your examples, should be NUMERIC(5,2) - meaning five numbers in total and 2 after the decimal point.