【函数格式】:
decode (
expression,
condition_01, result_01,
condition_02, result_02,
......,
condition_n, result_n,
result_default)
【函数说明】:
若表达式expression值与condition_01值匹配,则返回result_01,若不匹配,则继续判断;
若表达式expression值与condition_02值匹配,则返回result_02,若不匹配,则继续判断;
以此类推,若表达式expression值condition_n值匹配,则返回result_n,若不匹配,则继续判断;
若表达式expression值与以上所有的condition都不匹配,则返回默认值result_default,若省略result_default参数,则返回null
示例
insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud01', 'clas01', 60, 70, 80, 'C');
insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud02', 'clas01', 75, 85, 95, 'B');
insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud03', 'clas01', 80, 90, 100, 'A');
insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud04', 'clas02', 55, 60, 65, 'D');
insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud05', 'clas02', 40, 50, 60, 'E');
insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud06', 'clas02', 100, 100, 100, 'S');
查询表中数据,查询结果如下所示
1.将指定数据翻译或转换成其他形式
sql语句如下:
select GradeLevel,decode(GradeLevel,'S','完美','A','优秀','B','良好','C','较好','D','及格','E','不及格','其他') as gradeRemark from T_STUDENT_GRADE
返回结果如下图所示:
2.对指定数据按指定范围进行分段
sql语句如下:
select mathgrade,decode(mathgrade,100,'完美',decode(sign(mathgrade-90),1,'优秀',0,'优秀',-1,decode(sign(mathgrade-80),1,'良好',0,'良好',-1,decode(sign(mathgrade-70),1,'较好',0,'较好',-1,decode(sign(mathgrade-60),'1','及格',0,'及格',-1,'不及格'))))) as mathGradeRemark from T_STUDENT_GRADE
返回结果如下图所示:
3.判断指定数据是否符合指定特征
sql语句如下:
//判断英语分数是否及格
select engilshgrade,decode(sign(engilshgrade-60),1,'大于60分',0,'等于60分','低于60分') passFlag from T_STUDENT_GRADE
返回结果如下图所示:
文章来源地址https://www.toymoban.com/news/detail-685730.html
4.对数据按照指定要求进行排序
sql语句如下:
select * from T_STUDENT_GRADE order by decode(GradeLevel,'S',1,'A',2,'B',3,'C',4,'D',5,'E',6) asc
返回结果如下图所示:
文章来源:https://www.toymoban.com/news/detail-685730.html
到了这里,关于10.Oracle中decode函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!