Solved

SQL - turn a integer value to a string

Posted on 2012-03-15
3
164 Views
Last Modified: 2012-08-14
If I have a flag has a value (0, 1, 2, 3),  I will like to turn into a corresponding label (beginner, basic, intermediate and advanced)

Table 1
Person|SkillLevel
P1 | 0
P2 | 1
P3 | 2
P4 | 3

Like to get the result
P1 | beginner
P2 | basic
P3 | intermediate
P4 | advanced

How do I write such a sql statement
0
Comment
Question by:tommym121
3 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 37725397
select case when skillLevel = 0 then 'Beginner'
                  when skillLevel = 1 then  'Basic'
                   --repeat for other 2.
            END as SkillLevelText

from <table>


If you're going to be using this all over the place, create a scalar function, then you could call like:


select fnGetNameBySkillLevel(skillLevel)
from <table>
0
 
LVL 7

Assisted Solution

by:OklahomaDave
OklahomaDave earned 250 total points
ID: 37725451
A SQL purist designer would probably at least suggest a lookup table that maps the symbolic values to their literal values, then queries perform a JOIN from Table1 to the lookup table, eg

assuming values are in LookupTable,

select a.person, b.SkillLevelText
  from Table a
  join Lookuptable b
     on a.SkillLevel = b.SkillLevel
0
 

Author Closing Comment

by:tommym121
ID: 37725865
Thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now