在SQL Server结果集中选择并转换列值

时间:2022-01-02 21:19:54

Let's suppose i have a table called 'customer' in SQL Server 2008, with this result set :

假设我在SQL Server 2008中有一个名为“customer”的表,其结果集如下:

select * from customer

cu_id  cu_status
1      O
2      C
3      O

The 'cu_status' column is a code and i want to convert it to its correspondent value : 'O' is 'Open' and 'C' is 'Closed'. So my desired result set would be :

'cu_status'列是一个代码,我想将其转换为其对应的值:'O'是'Open','C'是'Closed'。所以我想要的结果集是:

cu_id  cu_status
1      Open
2      Closed
3      Open

Is there a way to do it only with a SELECT command ?

有没有办法只使用SELECT命令?

Thanks !

谢谢 !

2 个解决方案

#1


3  

There are several ways to do this. The simpler one would be using a CASE expression:

有几种方法可以做到这一点。更简单的是使用CASE表达式:

SELECT cu_id,
       CASE cu_Status
          WHEN 'O' THEN 'Open'
          WHEN 'C' THEN 'Closed'
       END cu_status
FROM dbo.customer;

#2


1  

Yes you can do the following:

是的,您可以执行以下操作:

SELECT CU_ID,
    CASE WHEN CU_STATUS = 'O' THEN 
        'Open'
    ELSE 
        'Closed'
    END AS CU_STATUS
FROM CUSTOMER

#1


3  

There are several ways to do this. The simpler one would be using a CASE expression:

有几种方法可以做到这一点。更简单的是使用CASE表达式:

SELECT cu_id,
       CASE cu_Status
          WHEN 'O' THEN 'Open'
          WHEN 'C' THEN 'Closed'
       END cu_status
FROM dbo.customer;

#2


1  

Yes you can do the following:

是的,您可以执行以下操作:

SELECT CU_ID,
    CASE WHEN CU_STATUS = 'O' THEN 
        'Open'
    ELSE 
        'Closed'
    END AS CU_STATUS
FROM CUSTOMER