• 示例:在查询中使用带搜索条件的 CASE 表达式
    1. Query OK, 0 rows affected (0.07 sec)
    2. obclient> INSERT INTO t_case2(id,c_date)
    3. VALUES (1,to_date('2019-03-01','yyyy-mm-dd'))
    4. ,(2,to_date('2019-05-08','yyyy-mm-dd'))
    5. ,(3,to_date('2019-07-07','yyyy-mm-dd'))
    6. ,(4,to_date('2019-10-11','yyyy-mm-dd'))
    7. ,(5,to_date('2019-12-12','yyyy-mm-dd'))
    8. ,(6,to_date('2020-01-05','yyyy-mm-dd'));
    9. Query OK, 6 rows affected (0.01 sec)
    10. obclient> SELECT id, c_date,
    11. CASE
    12. WHEN months_between(sysdate, c_date) > 12 THEN 'More than one year ago'
    13. WHEN months_between(sysdate, c_date) > 9 THEN 'More than three quarters ago'
    14. WHEN months_between(sysdate, c_date) > 6 THEN 'More than half a year ago'
    15. WHEN months_between(sysdate, c_date) > 3 THEN 'More than a quarter ago'
    16. WHEN months_between(sysdate, c_date) >= 0 THEN 'Within a quarter'
    17. ELSE 'Illegal'
    18. END "Duration"
    19. FROM t_case2;
    20. | ID | C_DATE | Duration |
    21. +----+-----------+------------------------------+
    22. | 1 | 01-MAR-19 | More than one year ago |
    23. | 2 | 08-MAY-19 | More than three quarters ago |
    24. | 3 | 07-JUL-19 | More than half a year ago |
    25. | 4 | 11-OCT-19 | More than a quarter ago |
    26. | 5 | 12-DEC-19 | More than a quarter ago |
    27. | 6 | 05-JAN-20 | Within a quarter |
    28. +----+-----------+------------------------------+
    29. 6 rows in set (0.00 sec)