Returns null for date_format when input is NULL in mysql

I am doing something like this:

SELECT date_format(mydate, '%d/%m/%Y') FROM xyz; 

When mydate is NULL, date_format returns 00/00/0000. This is correct, but how can I make it so that it returns NULL when the input is NULL?

+7
source share
4 answers
 SELECT IF(mydate,date_format(mydate, '%d/%m/%Y'),NULL) FROM xyz; 

Source: http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

+12
source

You can wrap this in IF -Clause, for example:

 SELECT IF(mydate,DATE_FORMAT(mydate, '%d/%m/%Y'),NULL) FROM xyz; 

However, if your mydate variable mydate not a date value, the request in your message should return (NULL) anyway.

+1
source
 case when date_format(mydate, '%d/%m/%Y') = 00/00/0000 then null else /// end as mydate, 
0
source
 select case when isnull(mydate) then null else date_format(mydate, '%d/%m/%Y') end as mydate from xyz; 
0
source

All Articles