Repository navigation
Detect an empty IndexOf pattern with LIKE instead of equality on SQL Server - #39495
Merged
AndriySvyryd merged 1 commit intoNov 3, 2026
Merged
Conversation
…Server
SQL Server ignores trailing spaces when comparing strings with =, so the
`@pattern = N''` check used to compensate CHARINDEX for an empty pattern
was also true for whitespace-only patterns. As a result,
s.IndexOf(" ") returned 0 instead of -1 when the space wasn't found (and
start instead of -1 with a starting position), and the found position was
off by one.
Use `@pattern LIKE N''` instead, as already done for the Contains
translation (dotnet#19402).
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The review record identifies no active issues blocking approval.
No open issues 🎉
What changed in this PR
This pull request updates SQL Server string.IndexOf translation to distinguish empty patterns from whitespace-only patterns, avoiding trailing-space comparison behavior.
Changes:
- Replaces equality-based empty-pattern detection with
LIKE N''. - Adds regression tests for whitespace-only patterns with and without a starting position.
- Updates SQL baselines for SQL Server, SQLite, Cosmos, and Northwind queries.
- The implementation still misclassifies whitespace-only patterns for ANSI-mapped (
varchar/char) strings; an exact length-based check or explicitly Unicode comparison is needed.
This pull request requires changes.
| File | Description |
|---|---|
test/EFCore.SqlServer.FunctionalTests/Query/Translations/StringTranslationsSqlServerTest.cs |
Updates SQL Server translation and regression baselines. |
test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs |
Updates the affected SQL Server ordering baseline. |
test/EFCore.Sqlite.FunctionalTests/Query/Translations/StringTranslationsSqliteTest.cs |
Adds SQLite baselines for the shared regression tests. |
test/EFCore.Specification.Tests/Query/Translations/StringTranslationsTestBase.cs |
Adds shared whitespace-pattern IndexOf regression tests. |
test/EFCore.Cosmos.FunctionalTests/Query/Translations/StringTranslationsCosmosTest.cs |
Adds Cosmos baselines for the shared regression tests. |
src/EFCore.SqlServer/Query/Internal/Translators/SqlServerStringMethodTranslator.cs |
Changes SQL Server empty-pattern detection from equality to LIKE, but does not yet handle ANSI-mapped strings correctly. |
Review effort: Lite
AndriySvyryd
approved these changes
Nov 3, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SQL Server ignores trailing spaces when comparing strings with =, so the
@pattern = N''check used to compensate CHARINDEX for an empty pattern was also true for whitespace-only patterns. As a result, s.IndexOf(" ") returned 0 instead of -1 when the space wasn't found (and start instead of -1 with a starting position), and the found position was off by one.Use
@pattern LIKE N''instead, as already done for the Contains translation (#19402).