Skip to content

Translate NULLIF #596

Closed

Description

@austindrenski

Per the docs:

NULLIF(value1, value2)

The NULLIF function returns a null value if value1 equals value2; otherwise it returns value1. This can be used to perform the inverse operation of the COALESCE example given above:

SELECT NULLIF(value, '(none)') ...

In this example, if value is (none), null is returned, otherwise the value of value is returned.

  1. Translate as extensions on DbFunctions:
public static T NullIf<T>(this DbFunctions _, T a, T b) where T : class 
    => Equals(a, b) ? default(T)  : a;

public static T? NullIf<T>(this DbFunctions _, T? a, T? b) where T : struct 
    => Equals(a, b) ? default(T?) : a;
  1. Consider translating simple ternary expressions:
a == b ? default(T)  : a;
a == b ? default(T?) : a;

Compatibility

Available for PostgreSQL 7.4 or later.

Activity

  1. added this to the 2.2.0 milestone on Aug 15, 2018
  2. roji commented on Aug 15, 2018

    @roji
    Member

    Don't you have it backwards in your implementation above? Shouldn't it be a == b ? null : a?

    Otherwise I like it... If we can get the ternary version to work well that would be great, but mind null vs. default... This could get a bit tricky.

  3. modified the milestones: 2.2.0, 3.0.0 on Dec 4, 2018
  4. modified the milestones: 3.0.0, Backlog on Aug 12, 2019
  5. modified the milestones: Backlog, 10.0.0 on Dec 11, 2024
  6. roji commented on Dec 11, 2024

    @roji
    Member

    @WhatzGames can you please comment here so you can be assigned for credit?

  7. added a commit that references this issue on Dec 11, 2024
    2397dd8
  8. WhatzGames commented on Dec 11, 2024

    @WhatzGames
    Contributor

    Sure! And thanks for the cleanup :)

  9. roji commented on Dec 12, 2024

    @roji
    Member

    After a second look, NULLIF is actually universally supported across databases and an issue already exists to track doing this in EF itself: dotnet/efcore#31682. So when I have a spare moment, I'll port this support into EF and remove it from EFCore.PG.

  10. added a commit that references this issue on Dec 18, 2024
    1f18573
  11. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions