conversion error while converting a decimal value. the record which has more precision are not converting - sql

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

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.

Redshift casting decimal to decimal

Why does casting a decimal to decimal lose all decimals? For example,
SELECT 1.5::DECIMAL
Returns 2
A decimal number has a precision (the total number of significant digits in the whole number) and a scale (the number of decimal digits).
In Redshift, the default precision is 18, but the default scale is 0, and automatic rounding is applied when casting, as explained in the documentation:
scale
The number of decimal digits in the fractional part of the value, to the right of the decimal point. [...] The default scale, if not specified, is 0.
[...]
If the scale of an input value that is loaded into a table is greater than the scale of the column, the value is rounded to the specified scale.
So you would need to specify a scale when casting, for example:
SELECT CAST(1.5 AS DECIMAL(10, 5))

Postgres eliminates decimal precision when casting a large float where first number in decimal portion <= 1 to real dtype

If you cast a large enough number with decimal precision to real where the first number in the decimal portion is less than or equal to 1 then the decimal portion is eliminated.
-- decimal precision is captured
-- 3 x 10^5 + 0.1
select 300000.1::real
OUTPUT:
300000.1
-- decimal precision is not captured
-- 3 x 10^6 + 0.1
select 3000000.1::real
OUTPUT:
3000000
-- decimal precision is captured
-- 3 x 10^6 + 0.2
select 3000000.2::real
OUTPUT:
3000000.2
I checked Postgres documentation and see that real dtype is "variable-precision, inexact" with a range of "6 decimal digits precision". I am not sure why decimal precision is captured in the third example but not the second since the size of the number is the same, just a larger decimal portion. Can someone explain this please?
That's just a coincidence.
real is stored in binary, so when you exceed the precision, it's the luck of the draw how far from the intended decimal number the value is.

how decimal digits before and after decimal point fixed in sql?

I have a variable of type decimal(26,16) and i want to save a value with 12 digits before decimal point and 14 digits after it, but it raises an "Arithmetic overflow error converting numeric to data type numeric". when i define a variable of this type, what does it really mean? it means it can only store values with 10 digits before and 16 digits after decimal point respectively or something else?
The highest number you can have in a decimal(26,16) is 9999999999.9999999999999999.
So when you try to store a 12 digit number it will to overflow
Try a decimal(28,16) instead
The maximum total number of decimal digits that can 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.
precision means the maximum number of digit you can use.
scale means the maximum number of digit you can use after decimal(.)
refer this...
http://msdn.microsoft.com/en-us/library/ms187746.aspx

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.