Solved

SQL - turn a integer value to a string

Posted on 2012-03-15
3
174 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 40

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
migration MS SQL database to Oracle 30 61
SQL: launch actions one before the other 10 23
SSRS Enable Remote Errors 4 25
performance query 4 22
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

772 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