I have a table with name, gender and address. Gender is stored as integer, 1 for male, and 2 for female. Now when I want to show the table, in PHP, during drawing the table I check for gender with (if -- else ) and change the (1,2) output into human language ("Male","Female").

Is there a way to do this formatting in the SQL query itself ? so that the query result are already formatted with 'male' and 'female' values.

(I need this to create a view of the formatted table, which I need to use it in DataTable ServerSide processing, so that when I can search in the view for "male", otherwise searching in original table for male shows 0 findings, because it is "1", not "male" ).


This question belongs on https://stackoverflow.com/, not here.

To answer your question, you'll want to use a CASE statement:

CASE [gender] WHEN 1 THEN 'Male' WHEN 2 THEN 'Female' END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.