Possible to represent NaN in scientific notation? - sql

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.

Related

How to get the NaN value in BigQuery?

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

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

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.

tsql coalesce with float and varchar

I have a float field which shows data as such:
1
1.00
3.12
3.00
I also have a varchar field that shows as such:
NA
ND
I
Data is as such: Fld_N is a float and Fld_S is varchar
Fld_N Fld_S
----- ------
1
ND
1.00
3.12
3
NA
I
Notice that a row can have a value for either the Fld_N or the Fld_S but not both.
What I am doing is using the coalesce as such:
COALESCE(STR(Fld_N,9,2), Fld_S) Fld
This doesn't quite work well as I have the decimal points always be upto 2 decimal points whereas I need it to support showing 1 as well as 1.00. Is there a way to not specify the decimal points and still accomomdate for showing 1 and 1.00 in my example?
try the convert function:
coalesce(convert(varchar,Fld_N),Fld_S) Fdl
Instead of STR, use
CAST(fld_N AS VARCHAR(9))
The VARCHAR will only use as many decimal places as necessary to show the value you provide.
Putting that into your COALESCE will yield:
COALESCE(CAST(fld_N AS VARCHAR(9)), Fld_S) AS Fld
In a float type column, there is absolutely no difference between 1 and 1.00. Therefore, what you suggest is actually impossible. The data stored in your database for rows 1 and 3 are, in reality, identical.
However, you can cast to VARCHAR instead of using STR, which will use the least number of decimal places necessary:
COALESCE(CAST(fld_N AS VARCHAR(12)), Fld_S) AS Fld
This should produce:
Fld
-----
1
ND
1
3.12
3
NA
I