Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How consider NULL as the MAX date instead of ignoring it in MySQL?

Tags:

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?

like image 399
user2741700 Avatar asked Nov 04 '13 08:11

user2741700


People also ask

Does SQL Max Ignore 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.

Can date be NULL in MySQL?

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 .

How do I ignore NULL values in select query?

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.

How do I stop NULL values in MySQL?

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.


1 Answers

Give this a shot:

SELECT ID, case when MAX(DATE IS NULL) = 0 THEN max(DATE) END AS DATE FROM test GROUP BY ID; 
like image 102
Samuel O'Malley Avatar answered Oct 05 '22 22:10

Samuel O'Malley