Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the Null Value from the result generated from MAX() used in query sql

When i use the query:

select MAX(DT_LOGDATE)
from   UMS_LOGENTRY_DTL
where  C_INPUTMODE='R' and VC_DEVICEID=10

the table gives maximum date, but if the condition is not satisfied it returns a data table with NULL. so there will be a null row. But I need it as empty data table. How I remove that null on noresult?

like image 827
Krish KvR Avatar asked May 21 '13 10:05

Krish KvR


People also ask

How do I remove NULL values in SQL?

There are two ways to replace NULL with blank values in SQL Server, function ISNULL(), and COALESCE(). Both functions replace the value you provide when the argument is NULL like ISNULL(column, '') will return empty String if the column value is NULL.

How do you avoid NULL values in Max function?

You can use COALESCE() along with aggregate function MAX() for this.

How does Max () handle NULL SQL?

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. MAX is a deterministic function when used without the OVER and ORDER BY clauses.

What does Max () mean in SQL?

The MAX() function returns the largest value of the selected column.


3 Answers

You could use HAVING clause:

SELECT MAX(DT_LOGDATE)
FROM   UMS_LOGENTRY_DTL
WHERE  C_INPUTMODE='R' and VC_DEVICEID=10
HAVING MAX(DT_LOGDATE) IS NOT NULL
like image 155
fthiella Avatar answered Oct 20 '22 10:10

fthiella


Use a derived table and filter on that:

select *
from (
  select MAX(DT_LOGDATE) as max_date
  from   UMS_LOGENTRY_DTL
  where  C_INPUTMODE='R' 
  and VC_DEVICEID=10
) t
where max_date is not null
like image 4
a_horse_with_no_name Avatar answered Oct 20 '22 10:10

a_horse_with_no_name


Scalar aggregates always return a single row even if aggregating an empty set.

You can also do

SELECT MAX(DT_LOGDATE)
FROM   UMS_LOGENTRY_DTL
WHERE  C_INPUTMODE = 'R'
       AND VC_DEVICEID = 10 
GROUP BY () 

The GROUP BY clause makes it a vector aggregate rather than scalar and no NULL row will be returned if there are no rows in UMS_LOGENTRY_DTL that match the WHERE.

like image 3
Martin Smith Avatar answered Oct 20 '22 10:10

Martin Smith