oracle case when的多种用法举例
时间: 2023-10-23 12:45:53 浏览: 126
oracle while的用法示例分享
1. 使用case when进行条件判断
CASE WHEN score >= 90 THEN 'A'
WHEN score >= 80 AND score < 90 THEN 'B'
WHEN score >= 70 AND score < 80 THEN 'C'
WHEN score >= 60 AND score < 70 THEN 'D'
ELSE 'F' END AS grade
2. 使用case when进行字段转换
CASE gender WHEN 'M' THEN 'Male'
WHEN 'F' THEN 'Female'
ELSE 'Unknown' END AS gender_transformed
3. 使用case when进行字符串处理
CASE WHEN length(name) > 10 THEN substr(name, 1, 10) || '...'
ELSE name END AS name_trimmed
4. 使用case when进行数值计算
CASE WHEN price < 10 THEN price * 0.9
WHEN price >= 10 AND price < 20 THEN price * 0.8
ELSE price * 0.7 END AS discounted_price
阅读全文