MySQL to select ENUM ('M', 'F') as either 'Male' or 'Female'

My field is ENUM for gender and saves β€œM” or β€œF”, but is there a way to select the field as β€œMale” or β€œFemale” based on the value?

+4
source share
5 answers
SELECT IF(gender='M','Male', 'Female') AS Gender 
+7
source

You can do things for him:

 SELECT CASE enum_field WHEN 'M' THEN 'Male' WHEN 'F' THEN 'Female' END 
+4
source

Yes, you can use the following query to do this.

 SELECT (CASE WHEN field = 'M' THEN 'MALE' WHEN field = 'F' THEN 'FEMALE' END) AS sex FROM table; 
+1
source

Use the CASE statement, for example:

 SELECT CASE FieldName WHEN 'M' THEN 'Male' ELSE 'Female' END FROM TableName 
0
source

Other technique

 select replace(replace(column,'M','Male'),'F','Female') from table 
0
source

All Articles