I want to display the decimal precision along with the result for decimal datatype in hive. However if there is no fraction part , in hive it will not display the decimal points.
hive> select cast(11 as decimal(14,2));
11
hive> select cast(11.22 as decimal(14,2));
11.22
In the above example instead of 11 it should display 11.00.How to achieve this?
Please help.
The following format_number() function should do it.Source, return type will be string though.
select format_number(11,2)
Note: Precision and Scale were added from Hive 0.13.
Use round or float_number to set the decimal point where u like , example :
select round(SUM(150.100 + 127.0090), 2);
or
select float_number(var,2);
Related
In Microsoft SQL Server 2005, why do the following commands produce integer results?
SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6
In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected.
If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result.
SELECT 151/CAST(6 AS DECIMAL (9,2))
Yes that is standard behavior
do
SELECT 151/6.0
or
SELECT 151/(CONVERT(DECIMAL(9,2),6))
or
SELECT 151/(6 * 1.0)
Because 151 and 6 are integers and you are doing integer division, even before the cast.
You need to make sure at least one of the arguments is a float type:
SELECT 151.0/6
Or
SELECT 151/6.0
Not a direct answer to your question. Still worth to take a look at Operators in Expressions if you need this in SSRS
/ Divides two numbers and returns a floating-point result.
\ Divides two numbers and returns an integer result.
Mod Returns the integer remainder of a division.
You need to give a placeholder for decimal places as well
Example
SELECT 151.000000/6
OR
SELECT 151/6.000000
Both will produce
25.16666666
For the same reason they would in C#, Java and other mainstream languages.
In integer arithmetic, the CAST is after the maths...
The CAST statement is a bit verbose. You can use the following instead:
DECLARE #TO_FLOAT FLOAT = 1.0;
SELECT (1 * #TO_FLOAT) / 2;
Or use a different multiplier type like DECIMAL if you prefer.
Try this:
SELECT 1.0*cast(151/6 AS DECIMAL(9,2))
SELECT 1.0*151/6
I want to separate the two numbers and get the answer with the remainder.
select 13/5;
I get an answer 2.
I add types.
select 13::numeric/5::numeric;
I get an answer 2.6000000000000000.
But I need only one number after the decimal point. How do I get 2.6?
Like so:
SELECT (13.0 / 5.0)::numeric(10, 1)
The .0 tells PG that it is a numeric literal. Division of two numerics result in numeric.
You can use ROUND to restrict to 1 decimal place. Note that you only need to cast the first value as numeric:
SELECT ROUND(13::numeric/5, 1)
Output
2.6
Try:
select round(13::numeric/5::numeric, 1);
https://www.w3resource.com/PostgreSQL/round-function.php
If you want it displayed in that fashion, you should convert the result to a string with the proper format:
SELECT to_char(13::numeric / 5::numeric, '9999.9999FM');
to_char
---------
2.6
(1 row)
I want to divide values and round them up to 8 decimal places but i found that some divisions return in scientific notation.
How can i always get round division without scientific notation?
select round( 123/100000000::decimal, 8 )
returns 0.00000123 as expected.
select round( 1/100000000::decimal, 8 )
returns 1e-8 but... i was expecting 0.00000001
How can i round 1/100000000 to 8 decimal places and return 0.00000001 ?
sql fiddle: http://sqlfiddle.com/#!15/9eecb7db59d16c80417c72d1e1f4fbf1/1534
Thanks for help.
best,
Actually, when I try your code in Postgres 9.3.4 using pgAdmin, both return the values you want. The values are not in exponential notation. Hence, I suspect this is an issue with your application, not the database.
An easy way to check is to put the value as a string:
select round( 1/100000000::decimal, 8 )::text
This should not return exponential notation.
Probably second case go beyond precision range 8.
You can check this Question Selecting floating point numbers in decimal form
Also what version are you working with because in pgAdmin 9.2 I get a different result
SELECT CAST ((SUM(r.SalesVolume)/1000) AS decimal(3,3)) FROM RawData r
The above is a part of a query that I am trying to run but returns an error:
Lookup Error - SQL Server Database Error: Arithmetic overflow error converting int to data type numeric.
Not sure what this means.
The result column looks like(Without dividing by 1000 and casting):
Total_Sales_Volume
64146
69814
68259
56318
66585
51158
44365
49855
49553
88998
102739
55713
Tried casting as float but doesnt help.
The Problem is decimal(3,3) --> this means a number with 3 digit, 3 of them behind the decimal point. If you want a number like this 1234567.123 you would have do declare it as decimal(10,3)
Try this:
SELECT CAST ((SUM(r.SalesVolume)/1000.0) AS decimal(6,3)) FROM RawData r
decimal(3,3) means that you allow numbers with 3 digits in total, and 3 of these are behind the comma ... I think you meant decimal(6,3)
EDIT: In addition, you need to to divide by 1000.0, not by 1000.
If you divide by 1000, it is an integer division.
If you divide by 1000.0, then it becomes a decimal division, with commas.
Try following:
SELECT CAST ((SUM(r.SalesVolume)/1000) AS numeric(6,3)) FROM RawData r
I have a couple floats that are kinda big. They're around a 100 million.
I would like this number to show like the following 123,456,789.01234
I've found that I can use CONVERT if its a money datatype but this doesn't do the full trick (it leaves off some decimal places).
I have to have commas on the left and five decimal places on the right.
Is there any built in SQL function to help with this? Or do I have to write a custom function?
Thanks
*** Update
I forgot to mention that I'm just displaying these as varchars. So there isn't any calculations after this.
This is running on an SQL database so MySQL and Oracle won't work.
DECLARE #f FLOAT
SET #f = 123456789.01234
SELECT LEFT('$' + CONVERT(VARCHAR(20), CAST(#f AS MONEY), 1), LEN(#f) - 2)
this will cut it up to two places of decimal for formatting. You can change LEN(#f) - 2 to modify this setting.
if you are just displaying this as text you can do the following:
oracle :
select to_char(123456789.01234,'999,999,999.99999') from dual; => 123,456,789.01234
MySQL :
select format(123456789.01234,5) => 123,456,789.01234<br>
the MySQL function rounds