SQL
SQL Server Case Statement
Case statement returns different values for different conditions
The following Product table is used to explain the concept:
ProductID | ProductName | Color | Price |
---|---|---|---|
1 | Bicycle | Red | 1000 |
2 | Motorbike | Blue | 10000 |
3 | Car | Green | 20000 |
4 | Computer | Black | 20000 |
SELECT
ProductID
,ProductName
,Case Color
When ‘Red’ then ‘R’
When ‘Blue’ then ‘B’
Else ‘Other’
End as Color
,Price
FROM Product
ProductID | ProductName | Color | Price |
---|---|---|---|
1 | Bicycle | R | 1000 |
2 | Motorbike | B | 10000 |
3 | Car | Other | 20000 |
4 | Computer | Other | 20000 |