Sam
Sam

Reputation: 103

Update SELECT query using CASE statement

I have a query like below.

SELECT Date
     , Co 
FROM Table1

I want to use a case statement to update Co = 'Client' when Co = 'XYZ'. I do not to update this info in the original Table1. I just want the change to reflect in my SELECT statement.

Table1 :

   Date   |  Co 
------------------
 1-1-2015 |  ABC
 1-2-2015 |  XYZ
 1-3-2015 |  AAA
 1-4-201  |  CCC

I want my SELECT statement result to look like below

   Date   |  Co 
------------------
 1-1-2015 |  ABC
 1-2-2015 |  Client
 1-3-2015 |  AAA
 1-4-201  |  CCC

Upvotes: 2

Views: 191

Answers (2)

potashin
potashin

Reputation: 44601

SELECT [Date]
     , CASE WHEN [Co] = 'XYZ' THEN 'Client'
            WHEN [Co] = 'PQR' THEN 'Partner'
            ELSE [Co] 
       END AS [Co]  
FROM [Table1]

Documentation

Upvotes: 5

Saba
Saba

Reputation: 385

In different syntax:

SELECT  [Date],
        CASE [Co]
            WHEN 'XYZ' THEN 'Client'
            WHEN 'PQR' THEN 'Partner'
            ELSE [Co]
        END AS [Co]
FROM Table1

Upvotes: 0

Related Questions