Replacing display values

Top  Previous  Next

SQL provides a function called CASE. It allows the result of a column to be determined by a the results of a case expression.

For example ServType variable returns A, N or C according to the service type (AI, natural mating or combination, respectively). If you want to display a readable text instead of the service type code:

SELECT
 ServDate,
 CASE ServType
   WHEN 'A' THEN 'Artificial insemination'
   WHEN 'N' THEN 'Natural mating'
   WHEN 'C' THEN 'Combination matings'
 END
FROM Service

 

Another example, the Score variable contains the score entered in the Physical Condition event. It is a numeric value, in this example from 0 to 100. You can assign a text category according the number, for example 0-49: bad, 50-79: good, 80-100: excellent. The sql statement would be:

SELECT
 CASE
   WHEN Score BETWEEN 0 and 49 THEN 'Bad'
   WHEN Score BETWEEN 50 and 79 THEN 'Good'
   WHEN Score BETWEEN 80 and 100 THEN 'Excellent'
 END
FROM FemaleEx

 

See any SQL manual for more information.