Skip to content

Detect an empty IndexOf pattern with LIKE instead of equality on SQL Server - #39495

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/sqlserver-indexof-whitespace-pattern
Nov 3, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/sqlserver-indexof-whitespace-pattern

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

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

  • [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

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

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.

🟢 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 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 merged commit a8f2298 into dotnet:main Nov 3, 2026
19 of 20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 3, 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