Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?


2 Answers

Use coalesce:

select coalesce(column_name, 'NULL VALUE') from the_table
like image 146
Thilo Avatar answered Sep 04 '25 23:09

Thilo


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

like image 20
Egor Plehanov Avatar answered Sep 04 '25 21:09

Egor Plehanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!