alex
alex

Reputation: 490403

How to return different strings from a Boolean type in MySQL?

If I have a column set up as Boolean in MySql, a query returns the value as either 0 or 1.

Is it possible to do something like this

SELECT `bool_value` AS "yes" OR "no"

What I mean is, return two different strings based on whether it is true or false.

Upvotes: 23

Views: 25671

Answers (3)

Will A
Will A

Reputation: 24998

SELECT CASE WHEN bool_value <> 0 THEN "yes" ELSE "no" END

Upvotes: 35

Chris Diver
Chris Diver

Reputation: 19842

You need the case statement.

SELECT (CASE WHEN column <> 0 THEN 'yes' ELSE 'no' END) As Value

http://dev.mysql.com/doc/refman/5.0/en/case.html

Upvotes: 32

mdma
mdma

Reputation: 57747

MySql supports the standdard SQL CASE statement, which other answers use. MySQL also has the shorter, but non-standard IF statement

SELECT IF(bool_value,'Yes','No')

See

Upvotes: 33

Related Questions