Converting data types in SQL case statement

I need to have the following happen in a select query when s.grade is a data type smallint.

CASE
WHEN s.grade = 13 THEN 'Grad'
ELSE s.grade
END as Grade

I haven't found any examples quite like mine where a smallint can be converted to a specific word varchar in a case statement.

Thanks
smantzDirector of TechnologyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Kyle AbrahamsSenior .Net DeveloperCommented:
WHEN s.grade = 13 THEN 'Grad' 
ELSE convert(varchar, s.grade)
END as Grade

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
smantzDirector of TechnologyAuthor Commented:
Not sure exactly how that works but it seems to have done the trick.  I'll do more checking to verify

Thanks
0
smantzDirector of TechnologyAuthor Commented:
Hey Thanks again,

I had to make an adjustment as, at 2009 the database went through some major conversions so, even graduated students up till last year showed as grade 12.

Solution: I used their Status field A (Active),  I (Inactive), G (Graduated)

WHEN s.Status = 'G' THEN 'Grad'
ELSE convert(varchar, s.grade)
END as Grade

That seemed to do the trick
0
Kyle AbrahamsSenior .Net DeveloperCommented:
when using a case statement they have to return the same DataType.  

(EG you can't do a varchar and an int).

Using the convert makes the int a varchar and sql will treat the column as a varchar column rather than an int column.
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
WHEN s.Status = 'G' THEN 'Grad'
ELSE convert(varchar, s.grade)
END as Grade

just a comment, wondering if you can do it with a mapping table, so that you can get rid of potential case ... when statements there, and create a single editing point for what should be returned for the Status field.
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
SQL

From novice to tech pro — start learning today.