Computer >> Computer tutorials >  >> Programming >> MySQL

Extract gender values as a string when it is stored in the table as a Boolean in MySQL


Let us first create a table −

mysql> create table DemoTable815(Gender BOOLEAN);
Query OK, 0 rows affected (0.84 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable815 values(true);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable815 values(false);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable815 values(false);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable815 values(true);
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable815;

This will produce the following output −

+--------+
| Gender |
+--------+
|      1 |
|      0 |
|      0 |
|      1 |
+--------+
4 rows in set (0.00 sec)

Following is the query to extract gender as a string when it is stored in the table as a boolean −

mysql> select if(Gender=true,'MALE','FEMALE') AS String from DemoTable815;

This will produce the following output −

+--------+
| String |
+--------+
| MALE   |
| FEMALE |
| FEMALE |
| MALE   |
+--------+
4 rows in set (0.00 sec)