I'm working with Amazon Athena and there are several columns that come out as scientific notation and I need it to come out as either an integer or a decimal. I've tried doing the answer from this: Convert exponential to number in sql
However, that doesn't work with SQL Presto, and this one doesn't work because I need it for multiple numbers, not just one: Query to convert exponential number to float in SQL Server
It should work if you cast the column to decimal.
SELECT CAST(doubleColumn AS decimal(22,2)) from my_table
There's no way to convert it globally.
Related
I have a field stored in float datatype. I need to convert it to numeric without it getting implicitly rounded in the process of conversion.
I have tried round(float_data,scale,1). Seems to work fine for most of the cases.but when the number of digits after decimal places is less than scale mentioned in round function it tries to floor down the number rather than appending 0 at the end.
For instance, round (0.0243,5,1) returns 0.02429. Why isn't it simply truncating the number to the number of digits mentioned?
I know this issue is when we use float as source datatype but I cannot change the source datatype.
The same truncation happens right when the same is achieved via ssis. Is there any way in sql to achieve this?
Because when converted to a float, the decimal 0.0243 is stored as 0.02429999969899654388427734375, which truncates to 0.02429. Looks like you want to round instead of truncate, eg
declare #f float = 0.0243
select round(#f,5,0)
I have a issue with round, trunc function from BigQuery standard query .
I expected at 3953.7, 3053.67, 3053.667. f1_, f2_ result is different. It is a bug??
I expected at 3.195, 3.195, 3.1955, 3.1965, 3.1945.
f1_, f3_ result is different. Is it my fault?
The ROUND() is used to round a numeric field to the nearest number of decimals specified.
There is a limitation of floating point values.
They can only represent binary values, but cannot precisely represent decimal digits after the decimal point (see here).
In case of SELECT ROUND(3053.665,2) you receive: 3053.66, you can overcome it by using: ROUND(value + 0.005, 2), which allows you to receive 3053.67.
Anyway, if you want to take care about precise decimal results, you should use the NUMERIC type. The following query gives results that you expect:
SELECT ROUND(3953.65,1), ROUND(numeric '3053.665',2), ROUND(numeric '3053.6665',3)
TRUNC(), the following query gives results that you expect:
SELECT TRUNC(3.1955,3), TRUNC(numeric'3.195',3), TRUNC(3.1955,4), TRUNC(numeric '3.1965',4), TRUNC(3.1945,4)
BigQuery parses fractional numbers as floating point by default for better performance, while other databases parses fractional numbers as NUMERIC by default. This means the other databases would interpret TRUNC(3.03,2) in the same way BigQuery interprets TRUNC(numeric '3.03',2).
I hope it will helps you.
This is due to the fact that, in BigQuery, digits are stored as floating point values by default.
You can fin more information about how these work in Wikipedia, but the main idea is that some numbers are not stored as they are but as the closest approximation its representation allows. If instead of 3.03 it is internally represented as 3.0299999999..., when you trunc it the result will be 3.02.
Same thing happens with round function, if 3053.665 is internally stored as 3053.6649999999..., the result of rounding it will be 3053.66.
If you specify it to be stored as NUMERIC, it then works as "expected":
select trunc(numeric '3.195', 3)
gives as result 3.195
You can find more information about Numeric Types in the official BigQuery Documentation.
Without using scientific notation, I need to convert a FLOAT to a string, without showing scientific notation, and capturing all possible precision.
For example, when I execute SELECT 1E0 / 1346E0 I get the following result:
This is how SQL Server displays a FLOAT value by default.
In this case, it displays 18 decimal places, which is more than the STR function can provide.
It also does not add any trailing zeros.
If SQL Server Management Studio can do this, can I also get this conversion in my code?
I need to avoid scientific notation at all costs, even if there are 20 leading zeros after the decimal point. A long string is not a problem.
Unfortunately, the CONVERT function does not provide what I need, even with style 3
try format()
SELECT
1E0 / 1346E0
, format(1E0 / 1346E0,'N18')
declare #float float = 0.000742942050520059
select cast(cast(#Float as decimal(38,35)) as varchar(200))
As was also noted, format works too, although I'm not a huge fan of it as it's a kind of heavy hitting CLR. but for one offs, it's fine.
This is on Microsoft SQL Server. We have a query where we are trying to join two tables on fields containing numeric data.
One table has the field defined as numeric(18,2) and the other table has the field defined as decimal(24,4). When joining with the native data types, the query hangs and we run out of patience before it will finish (left it running 6 min…). So we tried casting the two fields to be both numeric(18,2) and the query finished in under 10 seconds. So we tried casting the two fields to be both decimal(18,2) and again the query hangs. Does anyone know the difference between the decimal and numeric data types that would make them perform so differently?
DECIMAL and NUMERIC datatypes are the one and the same thing in SQL Server.
Quote from BOL:
Numeric data types that have fixed
precision and scale.
decimal[ (p[ ,s] )] and numeric[ (p[
,s] )] Fixed precision and scale
numbers. When maximum precision is
used, valid values are from - 10^38 +1
through 10^38 - 1. The ISO synonyms
for decimal are dec and dec(p, s).
numeric is functionally equivalent to
decimal.
From that, I'm surprised to hear of a difference. I'd expect the execution plans to be the same between the 2 routes, can you check?
Why are you using two datatypes to begin with? If they contain the same type of data (and joining on them implies they do), they should be the same datatype. Fix this and all your problems go away. Why waste server resources continually casting to match two fields that should be defined the same?
You of course may need to adjust the input variables for any insert or update queries to match waht you chose as the datatype.
My guess is that it's not a matter of a specific difference between the two data types, but simply the fact that SQL Server needs to implicitly convert them to match for the join operation.
I don't know why there would be a difference from your first query and the second, where you explicitly convert, but I can see why there might be a problem when you convert to a datatype that doesn't match and then SQL Server has to implicitly convert them anyway (as in your third case). Maybe in the first case, SQL Server is implicitly converting both to decimal(24,4) so as not to lose data and that operation takes longer than converting the other way. Have you tried explicitly converting the numeric(18,2) to a decimal(24,4)?
I have a number from an Oracle database of 47306832975095894070.85314746810624532. When I bring it into SQL Server, it certainly doesn't show that many digits. It shows as 4.73068329750959E+19, and the field is defined as FLOAT.
I think that probably includes all the significant digits, but I'm being asked if the number can be stored exactly as Oracle had it. Is there a another data type that will store ALL the digits? Is there a way in SQL Server 2005 to display the number not in exponential, but show all the digits stored?
Use decimal data type.
decimal(p,s) - p is a precision value, s is a scale value.
instead of float, use Decimal(38,17). This should allow you to store the number with the same precision that you had in Oracle.
The equivalent of the NUMBER(p, s) Oracle datatype on Sql Server is the numeric(p, s) datatype. Note that the default values for p (precision) and s (scale) are not the same on both platforms.
On Sql Server, a float represents a floating point number that is a whole different, approximate representation of a number. On Oracle, the equivalent would be BINARY_DOUBLE.