简体   繁体   中英

Case Expression for the Column

I have a query and I want to return the String instead of the Numbers, The Query has the value as DegreeID

select P.DegreeID, D.DegreeName, P.ProgramName
            from pagesite sp
                left join table2.Degrees D on P.DegreeID = D.DegreeID

I want to apply the case expression to the degreeID field like the swicth vase statement that if it is 1 name it like masters and if it is 2 , name it as bachelors and with default case of others

    select case
            when D.DegreeID= 1
             then 'masters'
            when D.DegreeID= 2
             then 'bachelors'
            else'others'
           end as ImNotSureTheColumnNameYouWantHere
      from pagesite sp
 left join table2.Degrees D 
        on P.DegreeID = D.DegreeID

All you need to do is to add CASE expression. I'm pretty sure there are tons of example online.

SELECT (
    CASE P.DegreeID
        WHEN 1 THEN 'masters'
        WHEN 2 THEN 'bachelors'
        ELSE 'Others'
    END
    ) AS Degree
    , D.DegreeName, P.ProgramName
FROM pagesite sp
LEFT JOIN table2.Degrees D ON P.DegreeID = D.DegreeID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM