Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle select max id from table returns null value

I have the query:

SELECT MAX(prod_id) FROM products;

It returns the maximum value if there are records. But, if I truncate table and run the same query I am unable to get the max id.

like image 510
user367134 Avatar asked Jan 01 '12 15:01

user367134


2 Answers

In case you want to query a table's column and suspect that the max function may return null, then you can return 0 in case null is encountered

SELECT NVL(MAX(P.PROD_ID), 0) AS MAX_VAL
 FROM PRODUCTS P

This will return at least 0 , if no value is encountered for the column that you mention ()

like image 172
Rohan Avatar answered Nov 20 '22 06:11

Rohan


Yes, by truncating the table you have removed all data in it, with no need for a commit. Therefore there is no data in the table and the max of nothing is nothing.

like image 5
Ben Avatar answered Nov 20 '22 04:11

Ben