------------------程序做得再好,数据有问题照样是个死 !

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

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

posted @ 2015-03-19 22:34  俊落笔如歌  阅读(582)  评论(0编辑  收藏  举报
           人的本事不是与生俱来的,不是你掌握了多少,而是当你面对一个未知问题的时候,你能用多少时间来掌握!       ---------俊落笔如歌