Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

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?

like image 243
Pathum Kalhan Avatar asked Nov 30 '25 04:11

Pathum Kalhan


2 Answers

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
like image 93
Sudhir Ojha Avatar answered Dec 02 '25 18:12

Sudhir Ojha


you can use a case when expression

select *, case when gender=false then 'Female' else 'Male' end as gender
from tablename
like image 32
Fahmi Avatar answered Dec 02 '25 20:12

Fahmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!