margherita pizza
margherita pizza

Reputation: 7175

MySQL extract gender as a string when it stored in the table as a Boolean

I have a table called accounts. There I have a column called gender where it's data type is Boolean.

I want to directly get the gender as a string in my select query. How do I achieve this in MySQL?

Upvotes: 3

Views: 910

Answers (3)

Rajat Kansal
Rajat Kansal

Reputation: 96

select gender,IF(gender=1,"Male","Female") as gender2 from accounts

Upvotes: 1

Sudhir Ojha
Sudhir Ojha

Reputation: 3305

MySql supports the standard SQL CASE statement. MySQL also has the shorter, but non-standard IF statement

SELECT IF(gender,'MALE','FEMALE') as gender from accounts

Upvotes: 5

Fahmi
Fahmi

Reputation: 37473

you can use a case when expression

select *, case when gender=false then 'Female' else 'Male' end as gender
from tablename

Upvotes: 4

Related Questions