Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server Error 103: The That Starts With Is Too Long

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

SQL Server Error 103 occurs when a LIKE pattern contains a prefix (the string of characters before the first wildcard) that exceeds 128 characters. To resolve this, you must shorten the search string before the first % or _ wildcard, or switch to a more robust search method like Full-Text Search.

What You'll Need

  • Access to the SQL Server Management Studio (SSMS) or the application source code generating the query.
  • Permissions to modify the T-SQL query or the stored procedure causing the error.
  • Knowledge of the specific column being queried to determine if a different search index is available.

Step-by-Step

  1. Identify the Offending Query: Locate the SELECT statement or stored procedure triggering the error. Look specifically for the WHERE clause using the LIKE operator.
  2. Analyze the Search Pattern: Examine the string passed to the LIKE operator. Error 103 is triggered specifically by the "prefix"—everything appearing before the first wildcard character. For example, in LIKE 'ThisIsAVeryLongString...% ', if the text before the % is longer than 128 characters, the error occurs.
  3. Shorten the Prefix: Reduce the length of the starting string to 128 characters or fewer. If the input is coming from a user-defined variable, use the LEFT function or a substring method to truncate the prefix.
  4. Move the Wildcard: If the exact starting sequence is not critical for the search, move the first wildcard character to an earlier position in the string. Changing LIKE 'LongString...%' to LIKE '%LongString...%' resolves Error 103 because the prefix becomes empty (0 characters), which is well within the limit.
  5. Implement a Length Check: If the query is part of an application, add logic to validate the length of the search term before it is sent to the database. If the term exceeds 128 characters, the application should either truncate the term or alert the user that the search term is too long.
  6. Test the Query: Execute the modified query in SSMS to ensure the error is resolved and that the results returned are still relevant to your needs.

Common Mistakes

  • Confusing Total Length with Prefix Length: Many users assume the entire LIKE string is limited to 128 characters. This is incorrect; the total string can be much longer, but the part before the first % or _ cannot exceed 128.
  • Using REPLACE blindly: Attempting to replace characters within the string without addressing the length of the prefix will not resolve the error.
  • Ignoring Case Sensitivity: When shortening a prefix, ensure that you are not accidentally removing characters that are essential for filtering in a case-sensitive collation environment.

Tips

  • Use Full-Text Search: For applications requiring searches on large blocks of text (like documents or long descriptions), implement SQL Server Full-Text Search. This removes the 128-character prefix limitation and significantly improves performance.
  • Leverage CONTAINS: If Full-Text Indexing is enabled, use the CONTAINS predicate instead of LIKE. It is designed for complex text searches and does not suffer from the Error 103 limitation.
  • Optimize Indexing: Remember that LIKE '%term%' (starting with a wildcard) prevents the SQL Server optimizer from using an index seek, resulting in a full table scan. If performance drops after moving the wildcard, consider a Full-Text Index.

Troubleshooting

If you have shortened the prefix and are still encountering issues, check the following:

  • Check for Hidden Characters: Ensure there are no hidden Unicode characters or trailing spaces in your variable that are contributing to the character count. Use LEN() to verify the actual length of the string being passed.
  • Verify Variable Types: Ensure the variable being passed into the LIKE clause is not being implicitly converted to a type that alters the character count or adds padding.
  • Inspect Nested Queries: If the LIKE statement is inside a view or a nested subquery, ensure the error is not being triggered by a different column or a different part of the execution plan.
  • Update Statistics: In rare cases, outdated statistics can lead to inefficient query plans that make the impact of a long prefix more apparent, though this usually affects performance rather than triggering Error 103.

Frequently Asked Questions

Why does SQL Server have a 128-character limit for LIKE prefixes?

This is a limitation of the internal engine used to process pattern matching and index seeks. The prefix is used to narrow down the range of the index to search; once the prefix exceeds 128 characters, the engine cannot process it using the standard LIKE mechanism.

Does this error apply to the _ wildcard?

Yes. The limit applies to any characters appearing before the first wildcard, whether that wildcard is a percent sign (%) or an underscore (_).

Will changing LIKE to = fix the error?

Yes, because the equals operator performs a literal comparison rather than a pattern match. However, this means the search will only find exact matches and will no longer function as a "starts with" or "contains" search.

Can I increase this limit in the SQL Server settings?

No. This is a hard-coded limit within the SQL Server database engine and cannot be changed via sp_configure or any other system setting.

Is there a performance difference between LIKE 'prefix%' and LIKE '%prefix%'?

Yes. LIKE 'prefix%' can utilize an Index Seek, which is very fast. LIKE '%prefix%' forces an Index Scan or Table Scan, which is significantly slower on large datasets.