Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL replace value on select

I have a column (SERVICE_ID) in my table where I can have only 3 values: 0, 1 and 2.

On a SELECT, I'd like to change those values into English words for display:

select client, SERVICE_ID
from customers

Currently displays:

| John     | 1
| Mike     | 0
| Jordan   | 1
| Oren     | 2

I'd like to change the query to get:

| John     | QA
| Mike     | development
| Jordan   | QA
| Oren     | management

There is any way to do this using only the select?

like image 807
Zelter Ady Avatar asked Dec 26 '22 15:12

Zelter Ady


1 Answers

SELECT client,
       CASE SERVICE_ID
         WHEN 0 THEN 'development'
         WHEN 1 THEN 'QA'
         WHEN 2 THEN 'management'
       END AS SERVICE
FROM   customers 

Though I'd have a separate table for Services with columns SERVICE_ID, Name and join onto that to retrieve the service name.

like image 168
Martin Smith Avatar answered Jan 11 '23 18:01

Martin Smith