I have a HSQL databse which is read by a 3rd party code. This 3rd party code translates certain numeric values to Double.NaN. My task is to write an sql query that can replace those numeric values with null.
At this item HSQLDB Statement and Java NaN doubles I read that sqrt(-1) or 0.0/0.0 will be translated to NaN. However, testing for equality works a bit odd for this values. Consider this query:
select
casewhen(1=1, 1, 0), -- this one is to make sure that casewhen works as we expect
casewhen(sqrt(-1)=sqrt(-1), 1, 0), -- this one looks good, we'll see
casewhen(sqrt(-1)=1, 1, 0) -- this is the odd part, we'll see
from mytable; -- any table with at least 1 row will do
It returns rows like this:
C1 C2 C3
1 1 1
Looks like sqrt(-1) is equal to every number. I could tweak it this way:
select
casewhen(1=1, 1, 0),
casewhen(
position(
'0E0/0E0' in
cast(sqrt(-1) to varchar(40))
) = 1,
'NaN', 'Numeric')
from mytable;
It returns what I need:
C1 C2
1 NaN
But I’m not satisfied: this solution needs a cast from numeric to varchar, then runs the position function and checks its return value to see if it would cast to NaN. Do you know a more effective solution on the SQL level?
I found a quicker workaround:
Will return rows like this:
So, the (x = x + 1) is only true for NaN values.