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
Invalid translation for constant negative TimeSpan for SQL Server #39222
When the query contains a constant negative TimeSpan, the translated SQL does not contain the negative sign.
This is probably because SQL Server time column cannot contain negative values.
This will result in the query executing without error and returning a wrong result.
This happens only for constants as parameters will cause an OverflowException.
I looked into this. The constant is generated by SqlServerTimeSpanTypeMapping.GenerateNonNullSqlLiteral, which uses '{0:hh\:mm\:ss}' (and the F variants). That custom format only prints the hours component, so the negative sign is lost (TimeSpan.FromHours(-1) becomes '01:00:00'). It likely drops the days component for values >= 24h as well, though I haven't verified that.
Since the time type can't represent negative values or values >= 24h, and parameters already throw an OverflowException, I'd suggest making the literal path consistent: throw a clear exception from GenerateNonNullSqlLiteral when the value is outside [0, 24h), instead of silently generating a wrong literal. I'd add cases for this in SqlServerTypeMappingTest.
Would the team be OK with that behavior, or would you prefer a different approach (e.g. translating such comparisons differently)? I'm happy to work on a PR if this is available.
Activity
Saad-elnagar commentedon Oct 13, 2026
I looked into this. The constant is generated by
SqlServerTimeSpanTypeMapping.GenerateNonNullSqlLiteral, which uses'{0:hh\:mm\:ss}'(and theFvariants). That custom format only prints the hours component, so the negative sign is lost (TimeSpan.FromHours(-1)becomes'01:00:00'). It likely drops the days component for values >= 24h as well, though I haven't verified that.Since the
timetype can't represent negative values or values >= 24h, and parameters already throw anOverflowException, I'd suggest making the literal path consistent: throw a clear exception fromGenerateNonNullSqlLiteralwhen the value is outside [0, 24h), instead of silently generating a wrong literal. I'd add cases for this inSqlServerTypeMappingTest.Would the team be OK with that behavior, or would you prefer a different approach (e.g. translating such comparisons differently)? I'm happy to work on a PR if this is available.
AndriySvyryd commentedon Oct 21, 2026
@Saad-elnagar Sounds good
Throw for out-of-range SQL Server TimeSpan literals (#39364)