SQL Server Error 288 occurs when the PATINDEX function is applied to a CHAR or NCHAR data type without proper handling. This error happens because PATINDEX expects a variable-length string type like VARCHAR or NVARCHAR to efficiently scan for patterns. CHAR and NCHAR are fixed-length types that pad spaces to their declared size, which can cause pattern matching to fail silently or throw this error. The fix is simple: convert the CHAR or NCHAR column to VARCHAR or NVARCHAR before using PATINDEX. This ensures accurate pattern detection and avoids the error.

What You'll Need

  • Access to a SQL Server instance with appropriate permissions to run SELECT queries
  • SQL Server Management Studio (SSMS) or another query tool like Azure Data Studio
  • A table containing one or more CHAR or NCHAR columns you intend to search with PATINDEX

Step-by-Step

  1. Identify the column causing the error. Review the full error message: it will specify which column or expression uses PATINDEX with a CHAR or NCHAR type. For example, if your query is SELECT PATINDEX('%abc%', MyCharColumn) FROM MyTable and MyCharColumn is defined as CHAR(50), this triggers the error.
  2. Locate the table schema. In SSMS, right-click the table in Object Explorer, select "Design", and note the data type of the column in question. Confirm it is CHAR, NCHAR, or another fixed-length character type.
  3. Modify the query to convert the column. Wrap the CHAR or NCHAR column in a CAST or CONVERT function to change it to VARCHAR or NVARCHAR. For example, change PATINDEX('%abc%', MyCharColumn) to PATINDEX('%abc%', CAST(MyCharColumn AS VARCHAR(50))) or PATINDEX('%abc%', CONVERT(VARCHAR(50), MyCharColumn)).
  4. Ensure the target length matches or exceeds the source. If your CHAR column is CHAR(100), use VARCHAR(100) or longer in the CAST. Using a shorter length may truncate data and cause false negatives in pattern matching.
  5. Test the corrected query. Run the query in your tool. The PATINDEX function should now return a numeric position (1-based index) or 0 if no match is found, without throwing Error 288.
  6. Apply the fix to all similar queries. Search your codebase or stored procedures for any other instances of PATINDEX used on CHAR/NCHAR columns and apply the same conversion.
  7. Consider altering the column type permanently if feasible. If this column is frequently used in pattern matching and rarely needs the fixed-length behavior, consider altering it to VARCHAR or NVARCHAR using ALTER TABLE MyTable ALTER COLUMN MyCharColumn VARCHAR(100). Always test this change in a non-production environment first.

Common Mistakes

  • Using PATINDEX directly on CHAR columns without conversion, assuming the function will handle padding automatically — it does not.
  • Using a CAST length that’s too short (e.g., CAST(MyCharColumn AS VARCHAR(10)) when the original is CHAR(50)), leading to data truncation and missed matches.
  • Confusing CHAR with VARCHAR in table design, leading to repeated errors. CHAR should only be used when all values are consistently the same length (e.g., country codes like 'US', 'CA'). Use VARCHAR for variable-length text.
  • Applying the fix only to one query but not others in the same application, leading to inconsistent behavior or recurring errors.
  • Assuming NCHAR can be converted to VARCHAR without consequences — this may lose Unicode characters if the target VARCHAR uses a non-Unicode collation. Use NVARCHAR instead for Unicode data.

Tips

  • Always use NVARCHAR when dealing with Unicode strings (e.g., international text, emojis, accents). Convert NCHAR to NVARCHAR, not VARCHAR, to preserve character integrity.
  • Use LTRIM to remove trailing spaces from CHAR columns before PATINDEX if the pattern doesn’t include spaces: PATINDEX('%abc%', LTRIM(CAST(MyCharColumn AS VARCHAR(50)))).
  • For performance, avoid applying CAST or CONVERT in WHERE clauses on large tables. Consider adding a computed column with the converted value and indexing it.
  • When writing new tables, default to VARCHAR or NVARCHAR unless you have a specific reason to use fixed-length CHAR/NCHAR (e.g., legacy systems, strict storage requirements).
  • Use the DATALENGTH function to check actual data length versus declared length: SELECT DATALENGTH(MyCharColumn), LEN(MyCharColumn) FROM MyTable — if they differ, padding is present.

Troubleshooting

If Error 288 persists after applying the conversion:

  1. Check for nested functions. If the column is passed through another function (like SUBSTRING or REPLACE), apply CAST/CONVERT to the final output before PATINDEX. Example: PATINDEX('%abc%', CAST(SUBSTRING(MyCharColumn, 1, 30) AS VARCHAR(30)))
  2. Verify collation. If you’re comparing Unicode and non-Unicode strings, ensure the collation is compatible. Use COLLATE Latin1_General_BIN if case-sensitive matching is needed.
  3. Test with a simple literal. Run SELECT PATINDEX('%abc%', CAST('abc ' AS VARCHAR(50))) — if this works, the issue is in your column reference, not the function.
  4. Check for NULL values. PATINDEX returns NULL if the input is NULL. Use ISNULL(CAST(MyCharColumn AS VARCHAR(50)), '') to prevent unexpected NULL results.
  5. Review the execution plan. If the query is slow after conversion, check for implicit conversions or table scans. Consider adding an indexed computed column.
  6. If using a view or function that references the column, alter that object too — the error may originate from a different query layer.

Frequently Asked Questions

Why does PATINDEX fail on CHAR columns but not VARCHAR?

PATINDEX scans for patterns and stops at the first non-matching character. CHAR columns are padded with trailing spaces to their full declared length. These spaces interfere with pattern matching, so PATINDEX sees the trailing spaces as part of the string and fails to find intended patterns.

Can I use REPLACE to remove spaces instead of CAST?

Yes — you can use PATINDEX('%abc%', REPLACE(MyCharColumn, ' ', '')) to remove all spaces. But be cautious: this removes spaces anywhere in the string, which may alter the meaning of your pattern. CAST to VARCHAR is safer and preserves internal spacing.

Does this error occur in Azure SQL Database?

Yes. Azure SQL Database uses the same SQL Server engine, so Error 288 behaves identically. The same conversion steps apply.

Is there a performance penalty for using CAST in every query?

Yes. Applying CAST on every row in a large table can slow down queries. For frequently used columns, create a computed column with the converted value and index it. Example: ALTER TABLE MyTable ADD MyVarCharColumn AS CAST(MyCharColumn AS VARCHAR(100)) PERSISTED.

What’s the difference between CAST and CONVERT here?

Functionally, they are interchangeable for this use case. CAST is ANSI-standard and more portable. CONVERT offers additional formatting options (e.g., date styles), but neither is needed here. Use CAST for simplicity.

Can I avoid this error by changing the pattern?

No. Even if you modify the pattern to include trailing spaces (e.g., '%abc %'), it’s unreliable because the padding length varies per row. The only reliable fix is converting the data type.