WHERE语句'THEN NULL'中的SQL条件CASE

时间:2018-10-09 07:04:41

标签: sql null case where

如果“ column_task”具有特定值,我尝试从“ operation_num”中替换值。但是我找不到合适的说法。你能帮我吗?

仅供参考:列“ operation_num”不包含任何NULL值。

WHERE
  CASE operation_num WHEN column_task = 'EXERCISE' THEN operation_num is null
                     ELSE operation_num -- operation_num = operation_num
  END

谢谢

6 个答案:

答案 0 :(得分:2)

尝试一下

WHERE
  CASE WHEN column_task = 'EXERCISE' THEN null
                     ELSE operation_num
  END

答案 1 :(得分:1)

将其转换为AND/OR(也会有更好的性能):

WHERE (column_task = 'EXERCISE' and operation IS NULL)
    OR operation_num

尽管我不了解最后的情况。

答案 2 :(得分:1)

我想就像下面的

WHERE
  (CASE column_task WHEN 'EXERCISE' THEN  null
                     ELSE operation_num end)= value

答案 3 :(得分:0)

对于我的错误提问,我们深感抱歉。我终于在查询的“选择”部分解决了我的问题:

select 
  (case when (to_char(operation_num) = '10' and column_task = 'EXERCISE')
  then REPLACE(to_char(operation_num),'10',' ')
  else to_char(operation__num)
  end) as "column"
  ,column_task
from  my_table

但是谢谢大家的努力。

答案 4 :(得分:0)

我认为您打算这样做

WHERE column_task <> 'EXERCISE' OR operation_num IS NULL)

答案 5 :(得分:0)

SELECT distinct ID,
       CASE
          WHEN COLUMN2 is not NULL then COLUM1||'#'|| COLUMN2
           
          ELSE COLUMN1
       END
FROM   table ;

此查询将结果显示为column1#,这是错误的,如果为空,则应显示列值