You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Translate DatePart.Microsecond and Nanosecond members #13481
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:
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).
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).
@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).
Activity
Kukkimonsuta commentedon Oct 4, 2018
I'd like to implement this. From what I could find, the following tables describe current possible translations:
I've included
DateDiffto see how the implementation matrix compares for more exotic parts (quarter, week..).microsecondandnanosecondgood enough or do we want to fill other blanks?DatePartMicrosecondandDatePartNanosecondtoEF.Functionsor do we also add other variants that are already covered byDateTime.XXX?smitpatel commentedon Oct 5, 2018
@divega to advise on above list.
cyptus commentedon Jun 6, 2019
is there any way to make use of datepart(week, ...) or ISO_WEEK right now?
Kukkimonsuta commentedon Jun 6, 2019
@divega I'm still interested in putting together PR, but I'd like to request advice on the requested API (see above)
cyptus commentedon Jun 7, 2019
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:
Query:
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).
18 remaining items
CRFricke commentedon Jun 19, 2024
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).
roji commentedon Jun 20, 2024
@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).
Add SqlServer DATEPART support for Microsecond and Nanosecond propert…
CRFricke commentedon Jul 10, 2024
@roji PR #34135 is awaiting review. Also, the Cosmos test runs are awaiting approval.
[-]Implement DatePart nanosecond[/-][+]Translate DatePart.Microsecond and Nanosecond members[/+]