tags:

views:

848

answers:

1

I'm trying to select some integer values in MySQL. Several of the values are zero, which I want to grab as an empty string, and grab the integer values when available.

So I have something like this:

SELECT CASE field WHEN 0 THEN '' ELSE field, [repeat for other fields]

Is there any way to shorten this in the SQL query? Does MySQL support the ternary operator?

+2  A: 

There's IF

select IF(field1=0,'',field1), ...

And if your fields are NULL, there's IFNULL

select IFNULL(field1,'')
nos
It also seems to be faster than CASE/WHEN.
Serkan Yersen