Lets say that I have a table named test like this:
ID DATE 1 '2013-01-26' 1 NULL 1 '2013-03-03' 2 '2013-02-23' 2 '2013-04-12' 2 '2013-05-02'
And I would like to get from this table :
ID DATE 1 NULL 2 '2013-05-02'
Here is my query:
select ID, max(DATE) from test group by ID
Problem is that MYSQL ignores NULL values and returns me
ID DATE 1 '2013-03-03' 2 '2013-05-02'
How can i do so when there is a NULL it takes the MAX as NULL?
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.
The . NET DateTime data type cannot handle NULL values. As such, when assigning values from a query to a DateTime variable, you must first check whether the value is in fact NULL .
SELECT column_names FROM table_name WHERE column_name IS NOT NULL; Query: SELECT * FROM Student WHERE Name IS NOT NULL AND Department IS NOT NULL AND Roll_No IS NOT NULL; To exclude the null values from all the columns we used AND operator.
Example - With SELECT Statement Here is an example of how to use the MySQL IS NOT NULL condition in a SELECT statement: SELECT * FROM contacts WHERE last_name IS NOT NULL; This MySQL IS NOT NULL example will return all records from the contacts table where the last_name does not contain a null value.
Give this a shot:
SELECT ID, case when MAX(DATE IS NULL) = 0 THEN max(DATE) END AS DATE FROM test GROUP BY ID;
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