T-SQL value to select

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

In a SELECT I would like to change these values ​​to English words to display:

 select client, SERVICE_ID from customers 

Currently displayed:

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

I want to change the request to get:

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

Is there a way to do this using only select?

+4
source share
2 answers
 SELECT client, CASE SERVICE_ID WHEN 0 THEN 'development' WHEN 1 THEN 'QA' WHEN 2 THEN 'management' END AS SERVICE FROM customers 

Although I will have a separate table for Services with SERVICE_ID, Name columns and join it to get the name of the service.

+9
source
 CREATE TABLE `serviceID_lookup` ( `serviceID` INT(10) NULL, `Description` VARCHAR(50) NULL ) COLLATE='utf8_general_ci' ENGINE=InnoDB; select c.client, sl.Description from customers c inner join serviceID_lookup sl on sl.serviceID = c.SERVICE_ID 
0
source

Source: https://habr.com/ru/post/1443940/


All Articles