SQL Server Error 406: PIVOT Operator Fix
In this article
SQL Server error 406 ("The type '...' cannot be used in the PIVOT operator") appears when a column in your PIVOT's source query has a data type that PIVOT does not support—most commonly a sql_variant, text, ntext, image, xml, or a CLR user-defined type. The fix is to cast or convert that column to a supported type (such as int, decimal, varchar, or nvarchar) inside the source subquery before PIVOT sees it. You do not need to change the PIVOT syntax itself; you need to change the data type of the values being pivoted.
What You'll Need
- Access to SQL Server Management Studio (SSMS), Azure Data Studio, or another client that can run T-SQL against the database.
- The
SELECTpermission on the source table or view. - The original query that produces the error, so you can identify which column is being pivoted.
- Knowledge of the intended data type for the aggregated values (for example, whether the result should be an integer count or a decimal sum).
Step-by-Step
- Read the full error message. SQL Server 406 names the column and its type, for example: The type 'sql_variant' cannot be used in the PIVOT operator. Write down that column name—it is the one you must convert.
- Locate the column in your PIVOT source. A PIVOT query has the form SELECT ... . The error refers to the value_column (the column being aggregated) or sometimes the pivot_column (the column whose values become new columns). Check both.
- Wrap the source query in a subquery that casts the offending column. For example, if
Amountissql_variant, change the inner query toSELECT Category, CAST(Amount AS decimal(18,2)) AS Amount FROM Sales. UseCASTorCONVERTto a type PIVOT supports: numeric types,char/varchar/nchar/nvarchar,date/datetime,uniqueidentifier, orbit. - If the pivot column itself is unsupported, cast it in the same subquery. For example,
CAST(Status AS varchar(50)) AS Status. PIVOT requires theFORcolumn to be a supported type as well. - Replace the original source with the corrected subquery in your PIVOT statement. Keep the alias and the
INlist unchanged unless the cast changes the literal values (for example, converting a number to a string changes[1]to['1']). - Run the query. If the error persists, repeat steps 1–3 for any other flagged column. PIVOT reports one offending column at a time, so you may need to fix several.
- Verify the output. Check that the pivoted columns contain the expected values and that no rows were dropped due to conversion failures. If a cast fails (for example, non-numeric text to
int), useTRY_CASTorTRY_CONVERTand decide how to handle NULLs.
Common Mistakes
- Casting after the PIVOT instead of before. The conversion must happen inside the subquery that feeds PIVOT; casting the outer result does not help.
- Forgetting the pivot column. Error 406 can point to either the aggregated column or the
FORcolumn. Check both. - Using
sql_variantdirectly.sql_variantis a common culprit in tables that store mixed types; always cast it to a concrete type. - Changing the
INlist incorrectly. If you cast a numeric pivot column tovarchar, theINlist must use string literals (e.g.,['2023']not[2023]). - Ignoring conversion errors. A cast that fails at runtime will abort the query; test the cast on a small sample first.
Tips
- Use
SELECT ... INTO #tempto materialize the casted source, then PIVOT the temp table—this makes debugging easier. - Prefer
TRY_CASTwhen the source column may contain values that cannot convert; it returns NULL instead of failing. - If you need to pivot
xmlortext, convert tonvarchar(max)first, but be aware PIVOT may still rejectmaxtypes in some versions—use a bounded length likenvarchar(4000). - Check the source view definition: a view may expose a column as
sql_varianteven if the base table column is not.
Troubleshooting
If the error persists after casting, run the inner subquery alone (without PIVOT) and inspect the column types using sp_help or SELECT * FROM sys.dm_exec_describe_first_result_set. Confirm that every column referenced in the PIVOT—both the aggregate and the FOR column—is a supported type. If you are using a CTE, the cast must be inside the CTE, not outside it. If you are on an older SQL Server version, some types (like date) may behave differently; test with a simple CAST first. When all else fails, rebuild the query from scratch using a temp table with explicit column types, then apply PIVOT to that temp table.
Frequently Asked Questions
What does SQL Server error 406 mean?
It means a column used in the PIVOT operator has a data type that PIVOT does not support, such as sql_variant, text, ntext, image, xml, or a CLR type.
Can I use PIVOT with a sql_variant column?
No. You must cast or convert the column to a supported type (e.g., int, decimal, varchar) inside the source subquery before PIVOT.
Does the error refer to the aggregated column or the pivot column?
It can refer to either. The message names the column, so check both the value being aggregated and the column in the FOR clause.
Why does my view cause error 406 when the table does not?
A view may implicitly convert a column to sql_variant or another unsupported type. Cast the column explicitly in the view or in the query that uses the view.
Can I use TRY_CAST with PIVOT?
Yes. TRY_CAST returns NULL for failed conversions, which PIVOT can handle, but you should decide whether NULL is acceptable for your aggregation.
Does the fix work on Azure SQL Database?
Yes. The same casting rule applies; Azure SQL Database uses the same PIVOT implementation as SQL Server.