Skip to content Skip to sidebar Skip to footer

How To Override Null Value From Aggregate Query Using Mysqldb Module In Python?

I am selecting the maximum date value from a MySQL database table in python using the MySQLdb module. If the result comes back as null, I want to override it with a default value.

Solution 1:

I could do this in the MySQL using a sub-query, but would prefer to do it in python.

Why do you say you need a subquery? You can just use COALESCE:

"select COALESCE(max(date_val), 'your_default_value_here') from my_table;"

Solution 2:

Try this:

min_date = cur.fetchone()[0]
min_date = min_date if min_date is not None else default_value

Post a Comment for "How To Override Null Value From Aggregate Query Using Mysqldb Module In Python?"