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
Solved

SQL - turn a integer value to a string

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
query question 12 32
tempdb log keep growing 7 33
Index and Stats Management-Specific tables 8 22
Use column to search string column 2 10
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

861 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