Home > Enterprise >  Using logical OR in IF condition of an MySQL query to check for NoneType or float
Using logical OR in IF condition of an MySQL query to check for NoneType or float

Time:09-16

I am trying to write an SQL query with an IF condition as follows -

IF (field_name1=0.0,0,ROUND(field_name1, 2)) as new_name,

When the query is run and the value of new_name is being consumed as metric in -

percentage_diffs = df_filtered[metric].pct_change().round(5) * 100

I am getting the following error -

TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

How do I check if the value of field_name1 is either 0.0 OR NoneType?

Thanks in advance.

CodePudding user response:

Make a OR clauue

CREATE tABLE tab1 (field_name1 DOUBLE)
INSERT INTO tab1 VALUES (NULL),(0.0)
SELECT 
IF ((field_name1 IS NULL OR field_name1=0.0),0,ROUND(field_name1, 2)) as new_name FROM tab1
| new_name |
| -------: |
|        0 |
|        0 |

db<>fiddle here

  • Related