1

I want to output a string depending on the value of int column. The query is below and language_id is an int column.

select email, language_id from tgf_mobileuser where email is not null;

What I want to do is get 'English' if language_id is 0, Japanese if language_id is 1.

Any help is greatly appreciated.

2
  • 1
    Instead of hard-coding this into the query, it would be better to have a language table that contains the language name for each ID. Then you can join with the table. Commented Sep 15, 2015 at 3:51
  • @Barmar Thank you for the useful tip Commented Sep 15, 2015 at 4:03

2 Answers 2

6

You can use CASE statement to achieve what you want:

select email, 
    CASE language_id WHEN 0 THEN 'English'
                     WHEN 1 THEN 'Japanese'
                     ELSE 'Unknown'
    END as Language
from tgf_mobileuser 
where email is not null;
Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest following something similar to this other post using an if clause in the select statement. 'IF' in 'SELECT' statement - choose output value based on column values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.