9

I'm 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?

4 Answers 4

16
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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

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

Comments

0
case 
    when date_format(mydate, '%d/%m/%Y') = 00/00/0000 then null
    else ///
end as mydate,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.