Tuesday, July 7, 2015

SQL: How to replace values of a field in the select result with some other values?

Let say that you have a student table, which has a column called GroundExam to indicate if the status of the student on the ground exam required by the education board. The column is a numeric field. The following is the meaning of the values.

0 : have not taken
1 : Passed
2 : Failed

Now, you would like to query the Student table to see the status of the students on the ground exam, but you would like the result to display the string which is more understandable than the numbers.

You may use the following query.

SELECT NAME, ID,
          CASE  GroundExam WHEN 0 THEN 'Have not Taken'
                    WHEN 1 THEN 'Passed'
                     WHEN 2 THEN 'Failed'
                    END AS GroundExam
FROM Students;

----------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment