Skip to content

Translate DatePart.Microsecond and Nanosecond members #13481

Description

@KLuuKer

We need access to SQL's DatePart nanosecond to do some in sql sorting (don't ask me why it's a stupid reason).

Would it be possible to have this in EF.Functions?

incase someone wants to lookup the command....
https://docs.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017

Activity

  1. added this to the Backlog milestone on Oct 3, 2018
  2. added
    help wanted
    on Oct 3, 2018
  3. Kukkimonsuta commented on Oct 4, 2018

    @Kukkimonsuta
    Contributor

    I'd like to implement this. From what I could find, the following tables describe current possible translations:

    DatePart Currently translated from Proposal
    year DateTime.Year
    quarter
    month DateTime.Month
    dayofyear DateTime.DayOfYear
    day DateTime.Day
    week
    weekday
    hour DateTime.Hour
    minute DateTime.Minute
    second DateTime.Second
    millisecond DateTime.Millisecond
    microsecond EF.Functions.DatePartMicrosecond
    nanosecond EF.Functions.DatePartNanosecond
    TZoffset
    ISO_WEEK
    DateDiff Currently translated from Proposal
    year EF.Functions.DateDiffYear
    quarter
    month EF.Functions.DateDiffMonth
    dayofyear
    day EF.Functions.DateDiffDay
    week
    hour EF.Functions.DateDiffHour
    minute EF.Functions.DateDiffMinute
    second EF.Functions.DateDiffSecond
    millisecond EF.Functions.DateDiffMillisecond
    microsecond EF.Functions.DateDiffMicrosecond
    nanosecond EF.Functions.DateDiffNanosecond

    I've included DateDiff to see how the implementation matrix compares for more exotic parts (quarter, week..).

    1. Is adding microsecond and nanosecond good enough or do we want to fill other blanks?
    2. Do we add only DatePartMicrosecond and DatePartNanosecond to EF.Functions or do we also add other variants that are already covered by DateTime.XXX?
  4. smitpatel commented on Oct 5, 2018

    @smitpatel
    Contributor

    @divega to advise on above list.

  5. added
    good first issue
    help wanted
    and removed
    help wanted
    good first issue
    on May 31, 2019
  6. cyptus commented on Jun 6, 2019

    @cyptus

    is there any way to make use of datepart(week, ...) or ISO_WEEK right now?

  7. Kukkimonsuta commented on Jun 6, 2019

    @Kukkimonsuta
    Contributor

    @divega I'm still interested in putting together PR, but I'd like to request advice on the requested API (see above)

  8. cyptus commented on Jun 7, 2019

    @cyptus

    I found an workaround. you can make use of the SQL datepart and datediff function by wrapping it with the DbFunctionAttribute.
    Tricky part is to tell ef core not to handle the datepart type parameter as a string.
    Example:

    DbContext:

    public int? DatePart(string datePartArg, DateTime? date) => throw new Exception();
    
    public void OnModelCreating(DbModelBuilder modelBuilder) {
        var methodInfo = typeof(DbContext).GetRuntimeMethod(nameof(DatePart), new[] { typeof(string), typeof(DateTime) });
        modelBuilder
            .HasDbFunction(methodInfo)
            .HasTranslation(args => new SqlFunctionExpression(nameof(DatePart), typeof(int?), new[]
                    {
                            new SqlFragmentExpression(args.ToArray()[0].ToString()),
                            args.ToArray()[1]
                    }));
    }

    Query:

    repository.Where(x => dbContext.DatePart("weekday", x.CreatedAt) == DayOfWeek.Monday);
    repository.Where(x => dbContext.DatePart("week", x.CreatedAt) > 1);
    ...

    more info: #9549

    watch out not to call the DbFunction method on an interface of DbContext. The call must happend directly on the DbContext instance (you can cast IDbContext to DbContext for the call).

  9. 18 remaining items

  10. CRFricke commented on Jun 19, 2024

    @CRFricke
    Contributor

    I'll have a go at this. Seems we need some trivial updates to the EFCore.SqlServer member translator classes for DateTime, TimeOnly, and TimeSpan and a bunch of tests.

    I assume it doesn't matter, but I'll mention it anyway. The .NET Millisecond, Microsecond, and Nanosecond properties return values between 0 - 999, whereas DATEPART returns the whole fractional time value (e.g. 0 - 999999999 for nanosecond).

  11. roji commented on Jun 20, 2024

    @roji
    Member

    @CRFricke sure, feel free to submit a PR. The general idea is that if we translate a built-in .NET member (like DateTime.Millisecond), the translation to SQL should return the same results as the .NET call does (with regards to the DATEPART behavior).

  12. added a commit that references this issue on Jul 1, 2024
    2c1b8fb
  13. CRFricke commented on Jul 10, 2024

    @CRFricke
    Contributor

    @roji PR #34135 is awaiting review. Also, the Cosmos test runs are awaiting approval.

  14. modified the milestones: Backlog, 10.0.0 on Oct 14, 2024
  15. changed the title [-]Implement DatePart nanosecond[/-] [+]Translate DatePart.Microsecond and Nanosecond members[/+] on Dec 15, 2024
  16. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-queryarea-sqlservercustomer-reportedgood first issuepreview-1

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions