码迷,mamicode.com
首页 > 其他好文 > 详细

CASE函数用法:相当于switch..case:,能够实现等值判断,也可以做范围判断 相当于if...else

时间:2015-03-19 23:51:26      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

--CASE函数用法:
--1.相当于switch..case:,能够实现等值判断
--语法:
--case 字段/表达式 --判断指定折字段或者表达式的值,它不能判断null值,因为null不是一个具体的值
-- when 值 then ‘自定义值‘
-- when 值 then ‘自定义值‘
-- else ‘前面都不满足的情况‘
--end
--特点:
-- 1.会生成一个新列
-- 2.then后面的值的类型需要一致
select StudentNo,StudentName,
case ClassId
when 1 then ‘一期班‘
when 2 then ‘2期班‘
when 3 then ‘3期班‘
when 4 then ‘4期班‘
when 5 then ‘5期班‘
else ‘我也不知道‘
end ,
borndate
from Student
--2.也可以做范围判断 相当于if...else
--语法:
--case --如果case后面没有任何的表达式或者字段,那么就可以实现范围的判断
-- when 条件表达式 then 值
-- 。。
-- else
--end
select StudentNo,StudentName,
case
when borndate>‘2000-1-1‘ then ‘少年‘
when borndate>‘1990-1-1‘ then ‘青年‘
when borndate>‘1980-1-1‘ then ‘中年‘
when BornDate is null then ‘年龄不知道‘
else ‘年龄有点大‘
end
from Student

--百分制转换为素质教育
select StudentNo,
case
when StudentResult>=90 then ‘A‘
when StudentResult>=80 then ‘B‘
when StudentResult>=70 then ‘C‘
when StudentResult>=60 then ‘D‘
when StudentResult<60 then ‘E‘
when StudentResult is null then ‘没有考试‘
end
from Result

CASE函数用法:相当于switch..case:,能够实现等值判断,也可以做范围判断 相当于if...else

标签:

原文地址:http://www.cnblogs.com/dianshen520/p/4351994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!