Devin Crossman
Devin Crossman

Reputation: 7592

SQL replace value

I have some data stored in a table about a client's language preference but it is stored as an integer where 8 = english and 9 = french etc. The reason for this I assume is that the integers are keys in another table that links them to the string describing them.

Unfortunately I only have access to the first table and not the other one or it would be a simple matter of doing a JOIN so is it possible to write a SELECT query that maps the 8 to English and 9 to French and so on?

I'm imagining something like this..

SELECT clientName, FUNCTION(languagePreference, ((8, "English"), (9, "French"))) 
FROM table

So instead of this..

clientName languagePreference

Dave          8    
Emmanuelle    9    
Luc           9    
John          8

I get this...

clientName languagePreference

Dave         English
Emmanuelle   French
Luc          French
John         English

Upvotes: 1

Views: 310

Answers (2)

Kaf
Kaf

Reputation: 33829

TSQL:

SELECT clientName, 
  CASE languagePreference
       WHEN 8 THEN 'English'
       WHEN 9 THEN 'French'
       ... --Go on like this if you have more languagePreference keys
  END as languagePreference
FROM table

Upvotes: 2

Francis P
Francis P

Reputation: 13655

Using TSQL, use a CASE

SELECT clientName, 
       CASE WHEN (languagePreference=8) THEN 
               'English' 
       ELSE 
            CASE WHEN (languagePreference=9) THEN
                'French'
            ELSE
                'Unknown'
            END
       END as languagePreference
FROM table

Upvotes: 1

Related Questions