PostgreSQL- Round REAL data type (yes, I know numeric exist) - sql

I know REAL data type is not accurate and normally for currency I should use numeric data type.
But, I'm asked to do some stuff and one of the conditions is that the data type is real.
When I try to do round((....),2) for example, I get that round function does not exist for this data type.
My question is, without converting, is there any function that can return a REAL value rounded to 0?
Many thanks!1

As you can see here it's no way to round without any type cast. It's only two kinds of function exists:
round(dp or numeric) - round to nearest integer
round(v numeric, s int) - round to s decimal places
Real = double precision. So you need to use convert anyway if you want to get some decimal places:
select round('123.456789'::real::numeric,2)
upd. Keep care about rounding+cast at big real numbers:
select round('12122156.567'::real::numeric, 2); --< rounding up to 6 digits, result = 12122200
select round('12122156.567'::real::DOUBLE PRECISION::numeric,2); --<< rounding result = 12122157
Or you can use round without decimal places:
select round('123.456789'::real)

round a numeric value to 0 after the dot?
ROUND(numeric_value, 0)

After investigation, converting to ::numeric is the only way around

Related

How do I convert this value 1.2851048260000018E7 (double) to int datatype in a SQL query?

When I run a SQL query, I get a big exponential value 1.2851048260000018E7 for the dollar amount. How to convert it to regular value?
I'm assuming you're not really looking for int, but rather a non-scientific notation. If you simply want dollars and cents use the following (since you didn't provide sample code/data, you'll have to adjust this for your query):
SELECT CAST(1.2851048260000018E7 AS decimal(18,2))
If you need more decimal digits for calculations, use whatever would be appropriate for scale with the syntax decimal(precision, scale), described here.
If you really are looking for an int datatype, that is dollars with cents rounded, use:
SELECT CAST(ROUND(1.2851048260000018E7, 0) AS int)
Use caution if leaving out the ROUND function. When you CAST a decimal to int, the number will be truncated at the decimal point, not rounded.

Round a value to two decimal places in SQL

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.

SQL - Convert number to decimal

I'm trying to convert a number to a decimal with two decimals places.
SELECT CONVERT(DECIMAL(10,2),12345)
The above would return 12345.00 but I'm trying to achieve 123.45
You need something like that:
SELECT CONVERT(DECIMAL(15,2),12345/100.0)
SELECT CONVERT(DECIMAL(10,2),CAST(12345 as float)/CAST(100 as float))
Correction: The premise is somewhat flawed, as the data type of a literal number without a decimal point is int, not numeric as implied by the question. In that case, you do need to convert the initial value to either numeric or decimal before dividing:
SELECT CONVERT(DECIMAL,12345)/100
or
SELECT CAST(12345 AS DECIMAL)/100
(cast is the SQL standard, so if you ever want to apply this to other databases, it would be the preferred method.)
Alternately, you can just add a decimal point to the divisor, as SQL server will return the more precise data type when doing arithmetic on heterogeneous types:
SELECT 12345/100.0
According to the documentation, the numeric data type is functionally equivalent to the decimal datatype, so there's really no reason to convert between the two. It seems that all you really want to do is divide the value you have by 100:
SELECT 12345/100

SQL set floating point precision

For a SQL int that is being converted to a float, how do I set the precision of the floating point number?
This is the selection I would like to truncate to two or 3 decimal places:
AVG(Cast(e.employee_level as Float))avg_level,
Thanks!
In TSQL, you can specify two different sizes for float, 24 or 53. This will set the precision to 7 or 15 digits respectively.
If all you want to do is truncate to a set number of decimal places, you can use ROUND, ie:
ROUND(AVG(CAST(e.employee_level as float)), 3)
As a general rule, you can't specify the number of digits after the decimal point for a floating-point number. Floating point data types store the closest floating-point approximation to any given value. The closest floating-point approximation is unlikely to have the number of digits you want. Although you might be able to suppress every digit after the third one, that will only change the appearance of the value, not the value itself.
Integers are a different story. An integer--stored, converted, or cast to a floating-point data type--will be stored exactly over a large range. Floating-point data types don't have to store any fractional units for integers.
I'd suggest, though that the best practice for you is to
avoid casting integers to floating-point if you don't need fractional units, or
cast integers to decimal or numeric if you do need fractional units, or
handle display issues entirely in application code.
I have had the same issue when calculating a percentage and needing a resulting string value.
Example: 68 is what % of 379
Result is a float = 17.9419525065900
You can cast/convert to Numeric with the Precision = 2 and get 17.94
If you need the value as a string you can then cast it as a VarChar if needed.
You can use Round() as well but in this case it only makes 17.9419525065900 = 17.9400000000000.
You can also use Ceiling() and Floor() to get the next highest or lowest integer.
Ceiling(17.9419525065900) = 18
Floor(17.9419525065900) = 17
Using these combinations you should be able to achieve a result in any format you need.

Converting this varchar to a decimal with the appropriate decimal point?

I've been playing with cast()s and such with this and can't seem to get things to work. I have a varchar string that's 18 characters long that I'd like to convert or cast to a decimal, with five decimal places. So for instance, this string:
00000001987600130
Would become 19876.00130
It's the case that I'll always have a 17 character string, with the last five characters reserved as the decimal place.
I've been playing with casts and converts but I'm not quite there. For instance, these statements get me (sort of) close but not exactly.
select CAST('00000001987600130' as bigint)/100000.0
select (convert(decimal(17,5),left('00000001987600130',12),0))
If you have a suggestion I'm happy to try it. Thanks!
This works fine for me:
SELECT CONVERT (decimal, '00000001987600130') / 100000
The reason why the first one didnt work is because the result of the CAST is an integer, and dividing an integer by 100000 rounds / truncates it (not sure which) so that it is still an integer.
To ensure you get what you want do a final CAST to ensure decimal(17,5) exactly
SELECT CAST((CAST('00000001987600130' AS decimal) / 100000) AS decimal(17,5))
Otherwise, the output type is not correct in scale or precision and may have effects later.