Oracle SQL,IF语句

时间:2017-12-07 14:08:17

标签: sql oracle

我正在从一张表中运行查询,该表中包含有关帐号,已结算费用,核销数字以及核销原因的信息;所以该表称为group_write_off和表标题,account_id,charge_settled_date,total_charge_amount,amount_written_off,write_off_reason。我想做的就是把所有事情都拉下来,所以

Select * from group_write_off

但是对于write_off_reason包含单词'Audit'的任何情况,我希望amount_written_off显示为0(零),对于我希望显示原始amount_written_off的所有其他情况。

非常感谢。

1 个答案:

答案 0 :(得分:5)

使用CASE表达式。

SELECT CASE WHEN write_off_reason='Audit' THEN 0 ELSE amount_written_off END
FROM group_write_off

另请尝试使用INSTR()函数

检查列write_off_reason中是否存在Audit字
SELECT CASE WHEN INSTR(write_off_reason,'Audit') = 0 THEN 0 ELSE amount_written_off END
FROM group_write_off

如果你想要更多的话

SELECT CASE WHEN INSTR(write_off_reason,'Audit') = 0 THEN 0 
       WHEN INSTR(write_off_reason,'admin') = 0 THEN 0 
       ELSE amount_written_off END
FROM group_write_off