Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

oracle - display sql custom text from table column result

let say i do select score from table1. this will return result

score
----
0
20
40

how to use case, to change the output to if 0->strongly not agree 20->agree 40->very agreed

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This does a simple translation of fixed values:

select score
       , case score
              when 0 then 'strongly not agree'
              when 20 then 'agree'
              when 40 then 'very agreed'
              else 'don''t know'
         end as txt
from your_table
/

If you are working with ranges the syntax is slightly different:

select score
       , case 
              when score between 0 and 19 then 'strongly not agree'
              when score between 20 and 39 then 'agree'
              when score >= 40 then 'very agreed'
              else 'don''t know'
         end as txt
from your_table
/

The ELSE branch is optional but I have included it to handle NULLs or unexpected values. If your data model enforces the appropriate constraints you might not need it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...