I try to select max value from table
SELECT MAX(cid) FROM itemconfiguration;
However when table itemconfiguration
is empty the MAX(cid)
statements is evaluated to NULL
while i need a number. How to handle this and treat NULL
as 0 ?
MAX ignores any null values. MAX returns NULL when there is no row to select. For character columns, MAX finds the highest value in the collating sequence.
So, NULL is better. An empty string is useful when the data comes from multiple resources. NULL is used when some fields are optional, and the data is unknown.
You can insert NULL value into an int column with a condition i.e. the column must not have NOT NULL constraints. The syntax is as follows. INSERT INTO yourTableName(yourColumnName) values(NULL);
Just use Coalesce or NVL to handle NULLs.
The following code will return 0 if MAX(cid)
is NULL
SELECT COALESCE(MAX(cid), 0) FROM itemconfiguration
SELECT NVL(MAX(cid), 0) FROM itemconfiguration;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With