peace_love
peace_love

Reputation: 6461

How can I get a date from mysql database if it is not null?

I want to select the value birthday from my database table. If it is not null the output should be a date. But if it is null, then the output should be empty.

    'SELECT *,
    case when isnull(mydate) then null else date_format(mydate, cats.bithday,"%d.%m.%y") end as birthday;
    FROM cats 
    LEFT JOIN dogs ON cats.dogs=dogs.id'

I get an error:

Syntax error or access violation: 1582 Incorrect parameter count in the call to native function 'date_format'

Upvotes: 0

Views: 576

Answers (2)

Vasyl Zhuryk
Vasyl Zhuryk

Reputation: 1248

SELECT *, ifnull(cats.bithday,'') as cats_birthday
FROM cats 
LEFT JOIN dogs ON cats.dogs=dogs.id

Result:

case (birthday is null)
    cats_birthday = ''
case (birthday is not null)
    cats_birthday = db-value

Upvotes: 3

C14L
C14L

Reputation: 12548

You are giving date_format three parameters, but it only expects two.

See the MySQL docs here

DATE_FORMAT(date,format)

Upvotes: 0

Related Questions