Repository navigation
Return the starting position for IndexOf with an empty string on SQL Server - #39231
Merged
AndriySvyryd merged 1 commit intoOct 14, 2026
Conversation
…Server string.IndexOf(string.Empty, startIndex) returns startIndex in .NET (and on SQLite and Cosmos), but SQL Server always returned 0: the constant empty pattern was special-cased to 0 ignoring startIndex, and for a non-constant pattern CHARINDEX returns 0 for an empty pattern whatever the start location, so the offset only brought it back to 0. Filters such as IndexOf(pattern, start) == start silently returned no rows. Return startIndex for a constant empty pattern, and offset CHARINDEX by -startIndex when a non-constant pattern is empty.
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
The empty-pattern check must distinguish whitespace-only parameter values to avoid incorrect matches.
0 open findings
What changed in this PR
Fixes SQL Server string.IndexOf translation for empty patterns to honor startIndex, matching .NET behavior.
Changes:
- Updates constant and parameterized empty-pattern translation.
- Adds shared and provider-specific regression tests.
- A moderate issue remains: whitespace-only parameter values can be treated as empty on SQL Server.
| File | Summary |
|---|---|
test/EFCore.SqlServer.FunctionalTests/Query/Translations/StringTranslationsSqlServerTest.cs |
Adds SQL Server regression baselines. |
test/EFCore.Sqlite.FunctionalTests/Query/Translations/StringTranslationsSqliteTest.cs |
Adds SQLite translation baselines. |
test/EFCore.Specification.Tests/Query/Translations/StringTranslationsTestBase.cs |
Adds shared regression tests. |
test/EFCore.Cosmos.FunctionalTests/Query/Translations/StringTranslationsCosmosTest.cs |
Adds Cosmos translation baselines. |
src/EFCore.SqlServer/Query/Internal/Translators/SqlServerStringMethodTranslator.cs |
Adjusts SQL Server IndexOf translation. |
🧠 Review effort: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
AndriySvyryd
approved these changes
Oct 14, 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.
…Server
string.IndexOf(string.Empty, startIndex) returns startIndex in .NET (and on SQLite and Cosmos), but SQL Server always returned 0: the constant empty pattern was special-cased to 0 ignoring startIndex, and for a non-constant pattern CHARINDEX returns 0 for an empty pattern whatever the start location, so the offset only brought it back to 0. Filters such as IndexOf(pattern, start) == start silently returned no rows.
Return startIndex for a constant empty pattern, and offset CHARINDEX by -startIndex when a non-constant pattern is empty.