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?
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.
You can use COALESCE() along with aggregate function MAX() for this.
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.
The MAX() function returns the largest value of the selected column.
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
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
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
.
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