How to get the NaN value in BigQuery? - sql

Is there a way to get a NaN value directly in BigQuery? For example, I'm trying to get all the 'special' float values BQ supports, but what's the way to generate the NaN?:
SELECT 1e500, -1e500, 1e-500, -1e-500, ???
-- Infinity -Infinity 0.0 0.0 NaN
Is the only way to do CAST('NaN' AS float64) ? Or is there a way to represent it with scientific notation?

Another options (few out of many similar - you got an idea)
SELECT IEEE_DIVIDE(0, 0), LOG(1, 1e500)
with output

Related

Possible to represent NaN in scientific notation?

Is there a way to represent NaN in SQL with some sort of 'special value' ? Or is it always necessary to use a cast from a string to represent it, for example:
SELECT CAST('NaN' AS FLOAT)
-- NaN
Is there any such value-construction I can use directly, such as:
SELECT 1.2345e-6789
-- NaN
You should be able to use 0.0/0.0: IEEE 754 requires that the division of zero by zero to result in NaN.

SQL Impala convert NaN values to NULL values

I have the following column in my table:
col 1
1
3
NULL
NaN
5
"Bad" aggregations return NaN instead of NULL and the variable is type DOUBLE in the end.
I want to have one type of missing values only, hence I need to convert NULL to NaN or the other way around.
My problem is that when I partition with a window function it does not recognize NaNs as equal to
NULLS and creates separate subgroups, which is something I do not want.
Any suggestions on how to convert them?

Transformation of units in SQL Server

I have a dataset, df, that has a column of values that are in MB. I would like to transform into TB.
MB
10000000
20000000
Desired
TB
9.09
18.18
Doing
select MB AS 'TB', (CONVERT([int],round([MB]/((1024)*(1024)),(0)))) AS TB from df
However, the result I get is
MB
0
0
I am still researching. Any suggestion is appreciated
/ is integer division in SQL Server.
It means, that for example
SELECT 4 / 5
will return 0.
But, if you write
SELECT 4 / 5.0
you'll get 0.8
5.0 is treated as decimal type and all values in the expression are converted to decimal and division is no longer integer.
So, you can use 1024.0 constant in the expression, and all the values in it will be converted to decimal type and division will not be integer.
In the question you say that you want to show results with two decimal places, so you should not convert result to int.
select
[MB]
,round([MB]/(1024.0*1024.0), 2) AS TB
from df

snowflake Computation for dividing 2 columns giving wrong values

I am exactly doing this Sum(2322933.99/1161800199.8)*
100
I should get
1.9 something but I am getting 64. Something
can anyone guide my y this division in snowflake giving wrong results
I tried them converting into decimal values and tried with Formula div0()
Nothing worked
I guess that your database table has 33 rows. So you get 33 * 1.9 (because of SUM), which is about 64.
My guess, with the few details that you gave us:
sum(x)/sum(y) is different than sum(x/y)
1/2 + 2/4 + 4/8 = 1.5
(1+2+4)/(2+4+8) = 0.5
Try writing sum(total gross weight)/sum(total cases filled) instead of sum(total gross weight /total cases filled).

When/Why does Oracle adds NaN to a row in a database table

I know that NaN stands for Not a Number. But, I have trouble understanding when and why Oracle adds this to a row.
Is it when it encounters a value less than 0 like a negative number or when its a garbage value.
From the documentaton:
The Oracle Database numeric data types store positive and negative fixed and floating-point numbers, zero, infinity, and values that are the undefined result of an operation—"not a number" or NAN.
As far as I'm aware you can only get NaN in a binary_float or binary_double column; those data types have their own literals for NaN too, and there's an is nan condition for them too, and the nanvl() function to manipulate them.
An example of a way to get such a value is to divide a zero float/double value by zero:
select 0f/0 from dual;
0F/0
----
NaN
... so if you're seeing NaNs your application logic or underlying data might be broken. (Note you can't get this with a 'normal' number type; you get ORA-01476: divisor is equal to zero unless the numerator is float or double).
You won't get NaN for zero or negative numbers though. It's also possible you have a string column and an application is putting the word 'NaN' in, but storing numbers as strings is a bad idea on many levels, so hopefully that is not the case.
Nope <=0 is still a number so not quite. NaN (or infinity) are special values that the DB uses to keep it's sanity when dealing with non-computable numbers (+-∞, or simply something that is not a number).
Here's some code:
DECLARE
l_bd_test binary_double;
l_int_test INTEGER;
BEGIN
l_bd_test := 'NAN';
l_int_test := 0;
IF l_bd_test IS NAN THEN
DBMS_OUTPUT.PUT_LINE(l_bd_test || ' IS NAN');
ELSE
DBMS_OUTPUT.PUT_LINE(l_bd_test || ' IS A #');
END IF;
IF l_int_test IS NAN THEN
DBMS_OUTPUT.PUT_LINE(l_int_test || ' IS NAN');
ELSE
DBMS_OUTPUT.PUT_LINE(l_int_test || ' IS A #');
END IF;
END;
/
Substitute NAN for INFINITY or even negate it and see the results.