Rename value of a field in a Select statement

My where statement consists of numbers and I will like to reman these value in the report
field.  

For example
Select p.lastname, p.firstname, b.job
from p and b
where b.job in ('28', '30', '45', '2')

I would like the values in the b.job to not be the #s but if it is 28 then Nurse, if 30 then support, if 45 then manager, if 2 then tech.....
trinisunsetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
That requires a "CASE expression" like this:

SELECT
  p.lastname
, p.firstname
, b.job
, CASE
           WHEN b.job = 28 THEN 'Nurse' 
           WHEN b.job = 30 THEN 'support'
           WHEN b.job = 45 THEN 'manager' 
           WHEN b.job = 2  THEN 'tech'
           ELSE 'unknown'
  END AS your_alias
FROM p
INNER JOIN b ON .......
where b.job in ('28', '30', '45', '2')

Open in new window

0
PortletPaulfreelancerCommented:
BY the way, you can also use case expression in the ORDER BY clause, so just as an example if you wanted to sort by: manager/Nurse/support/tech then:
SELECT
  p.lastname
, p.firstname
, b.job
, CASE
           WHEN b.job = 28 THEN 'Nurse' 
           WHEN b.job = 30 THEN 'support'
           WHEN b.job = 45 THEN 'manager' 
           WHEN b.job = 2  THEN 'tech'
           ELSE 'unknown'
  END AS your_alias
FROM p
INNER JOIN b ON .......
WHERE b.job in ('28', '30', '45', '2')
ORDER BY
  CASE
           WHEN b.job = 28 THEN 'Nurse' 
           WHEN b.job = 30 THEN 'support'
           WHEN b.job = 45 THEN 'manager' 
           WHEN b.job = 2  THEN 'tech'
           ELSE 'unknown'
  END
, p.lastname
, p,firstname

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
Thanks.

I should also point out a little trick for the ORDER BY...

In the select clause you can give columns an alias, and you would need to do this for a case expression.

e.g. select case when a = 1 then 'fred' end AS MYCOL ...

You can (as I said before) repeat that case expression in the order by - but instead you are also allowed to use that column alias,

e.g. select case when a = 1 then 'fred' end AS MYCOL ... order by MYCOL

[but note you could not use MYCOL in the where clause, only in the order by]
hope that's not confusing.

Cheers, Paul.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.