user833129
user833129

Reputation:

Is there a function equivalent to the Oracle's NVL in MySQL?

I'm selecting the max of a column from a table. But there is one problem: if there are no rows in the table, it returns null.

I want to use a function which will return a certain value if the result is null. For example with Oracle there is the NVL function which gives a certain value if the column is null. Is there an equivalent function in MySQL ?

Upvotes: 31

Views: 61909

Answers (2)

Egor Plehanov
Egor Plehanov

Reputation: 11

or you can use IFNULL(expr1,expr2)

If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.

select IFNULL(column_name, 'NULL VALUE') from the_table;

taken from: https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_ifnull

Upvotes: 1

Thilo
Thilo

Reputation: 262852

Use coalesce:

select coalesce(column_name, 'NULL VALUE') from the_table

Upvotes: 56

Related Questions