Q

Use CASE to translate a value to a string

Is it possible in SQL to substitute a string if a certain value is returned from a query? For example,

select emp_id from emp_tb

returns

1
2
3

Would it be possible to replace 1 with John and 2 with Mary to give the end result of

John
Mary
3

Yes, you can stuff a whole raft [syn: batch, deal, flock, good deal, great deal, hatful, heap, lot, mass, mess, mickle, mint, muckle, peck, pile, plenty, pot, quite a little, sight, slew, spate, stack, tidy sum, wad, whole lot, whole slew] of translations into the SELECT if you wish...

select case emp_id 
            when 1 then 'John'
            when 2 then 'Mary'
            else cast(emp_id as char(4))
        end as emp_string 
  from emp_tb

Note that some database systems may require the CAST, but some don't, and will set the datatype of the CASE expression from the first defined expression inside the CASE.


This was last published in August 2004

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close