Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 987
alex Avatar asked Jul 29 '10 03:07

alex


People also ask

Can MySQL return Boolean?

MySQL doesn't really have booleans. TRUE and FALSE are aliases to 1 and 0, and the BOOL column type is just an alias for TINYINT(1) . All expressions that appear to give boolean results actually return 0 or 1.

Does MySQL support Boolean data type?

MySQL does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT). When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.

How do I change a Boolean value in SQL?

You can update boolean value using UPDATE command. If you use the BOOLEAN data type, MySQL internally convert it into tinyint(1). It can takes true or false literal in which true indicates 1 to tinyint(1) and false indicates 0 to tinyint(1).

Why MySQL Boolean is Tinyint?

In MySQL, TINYINT(1) and boolean are synonymous. Because of this, the MySQL driver implicitly converts the TINYINT(1) fields to boolean if the the Java tinyInt1isBit configuration property is set to true (which is the default) and the storage size is 1. Stitch then interprets these columns as BIT(1)/boolean .


1 Answers

SELECT CASE WHEN bool_value <> 0 THEN "yes" ELSE "no" END
like image 54
Will A Avatar answered Oct 07 '22 00:10

Will A