My current query works just fine outside of the CASE WHEN:
select *, timestampdiff(DAY, from_unixtime(laststaffreplytime, '%Y-%m-%d'),
curdate()) from swtickets;
However, when this is placed in the ELSE of a CASE WHEN, the column produces all null values:
SELECT *, CASE laststaffreplytime
WHEN 0 THEN 'No Response'
ELSE (timestampdiff(DAY, from_unixtime(laststaffreplytime, '%Y-%m-%d'),
curdate())) END as LastContact from swtickets;
Any ideas as to why this query is returning only null values?
The CASE operator has to be able to return a single data type — your sample returns a string in one case, and an integer in the other. I don’t think that should turn the results into NULLs — I would expect that the integers would be represented as strings in that case — but anything’s possible with MySQL 🙂
Try changing the “No Response” to a fixed integer value, like -1, to see if that changes the results.
From the manual: