Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL - turn a integer value to a string

Posted on 2012-03-15
3
Medium Priority
?
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 1000 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 1000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

722 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