ZakTaccardi
ZakTaccardi

Reputation: 12467

How can I alias returned values in a query?

I have a query that ultimately will return a column that might have the value "1", "2", or "3", where 1 means RED, 2 means BLUE, and 3 means GREEN.

How can I modify the query so instead of returning 1, 'RED' is returned, for 2 'BLUE' is returned, etc

I want my result to be something like:

COLOR

RED

RED

GREEN

BLUE

Instead of :

COLOR

1

1

3

2

Upvotes: 1

Views: 270

Answers (2)

DCookie
DCookie

Reputation: 43533

Alternatively, and more standards conformant:

SELECT CASE WHEN MyField = 1 THEN 'RED'
            WHEN MyField = 2 THEN 'BLUE'
            WHEN MyField = 3 THEN 'GREEN'
            ELSE 'UNDEFINED'
       EMD myfieldColor
FROM myTable;

It's also more flexible, as you can use a wider assortment of SQL in the WHEN clause. For example:

WHEN MyField = 2 AND Lang = 'SPANISH' THEN 'AZUL'

Upvotes: 3

xQbert
xQbert

Reputation: 35323

SELECT Decode(MyField, 1, 'RED'
                       2, 'BLUE'
                       3, 'GREEN'
                          'UNDEFINED' as myfieldColor

FROM myTable

Upvotes: 2

Related Questions