Search code examples
sqlnullcasesybase

SQL: where [columnName] = CASE WHEN (condition) THEN NULL ELSE [columnName] END


Want to run a query in Sybase to do the following:

When condition is true, only select records where columnName is null, otherwise, anything else. However, we know that to compare NULL, " = NULL" is not right, we should use "IS NULL" instead.

How can I integrate "IS NULL" to the query then?

select * from tableName
where columnName = CASE WHEN (condition) THEN NULL ELSE columnName END

Solution

  • You could try this alternative query that doesn't use CASE:

    select * from tableName
    where ((condition) AND columnName IS NULL) Or (Not (condition))