Return null for date_format when input is null in mysql

浪尽此生 提交于 2019-12-01 16:15:19
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

You can wrap this into a IF-Clause, like this:

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

That said, if your variable mydate is not a date value, the query in your post should return (NULL) anyway.

select case when isnull(mydate) then null else date_format(mydate, '%d/%m/%Y') end as mydate from xyz;
Light
case 
    when date_format(mydate, '%d/%m/%Y') = 00/00/0000 then null
    else ///
end as mydate,
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!