Let's assume now when developing a ETL, this column need to be convert to 'colorcode' to display the color description as number.
We could use CASE WHEN.. THEN in SQL to convert the table for reporting.
In this situation, using CASE WHEN..THEN is better than just IF..THEN because CASE WHEN could easily setup multiple conditions.
In this example we have to place a color code for black, silver, and red in our product table:
USE AdventureWorks2008
GO
SELECT Name,CASE WHEN ([COLOR]='BLACK') THEN 1
WHEN ([COLOR]='SILVER') THEN 2
WHEN ([COLOR]='RED') THEN 3
ELSE 0 END AS COLORCODE
FROM
Production.ProductAfter we run the query, notice that the column 'colorcode' has converted color name into integer numbers:
No comments:
Post a Comment