Skip to content

Return the starting position for IndexOf with an empty string on SQL Server - #39231

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/sqlserver-indexof-empty-string-start-index
Oct 14, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/sqlserver-indexof-empty-string-start-index

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

…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.

  • [x ] I've read the guidelines for contributing and seen the walkthrough
  • [ x] I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • [x ] The code builds and tests pass locally (also verified by our automated build checks)
  • [x ] Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • [ x] Tests for the changes have been added (for bug fixes / features)
  • [x ] Code follows the same patterns and style as existing code in this repo

…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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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 AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd AndriySvyryd changed the title Return the starting position for IndexOf with an empty string on SQL … Return the starting position for IndexOf with an empty string on SQL Server Oct 14, 2026
@AndriySvyryd
AndriySvyryd merged commit f01f1ea into dotnet:main Oct 14, 2026
19 of 20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants