• 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

    人的本事不是与生俱来的,不是你掌握了多少,而是当你面对一个未知问题的时候,你能用多少时间来掌握!
  • 相关阅读:
    C++宏定义详解
    编写Qt Designer自定义控件 MyPlugins
    关于MFC共享DLL的模块状态切换 .
    QT 与 MFC 的区别 .
    typedef
    C++ floor函数
    C++ floor函数 截断浮点数小数部分 转
    MFC的多国语言界面的实现 转
    新工作 Day16 周五
    新工作 Day15 周四
  • 原文地址:https://www.cnblogs.com/dianshen520/p/4351994.html
Copyright © 2020-2023  润新知