SQL Server Error 421: Data Type Cannot Be Selected
In this article
SQL Server Error 421 occurs when you attempt to select or return a data type that the database engine cannot process in the current context, typically because the data type is not supported for that specific operation. To fix this, you must use the CAST() or CONVERT() functions to transform the unsupported data type into a compatible one, such as converting a custom user-defined type or a complex binary object into a standard string or integer.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or another SQL query editor.
- The specific SQL query or stored procedure that is triggering the error.
- Permissions to view the table schema to identify the exact data types of the columns being selected.
- Knowledge of the target data type required for your application or report.
Step-by-Step
- Identify the Culprit Column: Examine the SELECT statement in your query. Look for columns that use non-standard data types. Common offenders include
text,ntext,image,xml, or user-defined types (UDTs). If you are selecting*, replace it with a list of specific column names to isolate which individual column is causing the failure. - Verify the Data Type: Right-click the table in the Object Explorer of SSMS and select Design, or run the query
EXEC sp_help 'YourTableName'. Note the exact data type of the column causing the error. For example, if the column is defined asimage, it cannot be selected in certain contexts (like some types of views or specific aggregate functions) without conversion. - Determine the Target Type: Decide what the data should look like in the output. If you have a large binary object (BLOB) that you need to read as text, your target type would be
varchar(max)ornvarchar(max). If you are dealing with a custom type that represents a number, your target would beintordecimal. - Apply the CAST Function: Wrap the problematic column in a CAST function. The syntax is
CAST(column_name AS target_type). For example, if you have a column namedOldNotesof typetext, change your query fromSELECT OldNotes FROM TabletoSELECT CAST(OldNotes AS varchar(max)) FROM Table. - Apply the CONVERT Function (Optional): If you need more control over the formatting (especially for dates or currency), use CONVERT. The syntax is
CONVERT(target_type, column_name, style). This is particularly useful when converting binary data to a specific hex string format. - Test the Query: Execute the modified query. If the error persists, check if the column is being used inside a
DISTINCT,GROUP BY, orORDER BYclause. These clauses often have stricter requirements for data types than a simple SELECT list. - Update the Application Code: Once the query works in SSMS, update the corresponding code in your application or stored procedure to ensure the fix is permanent.
Common Mistakes
One of the most frequent mistakes is attempting to cast a data type to something fundamentally incompatible, such as trying to cast an image type directly to an int. This will not resolve Error 421; it will simply replace it with a "Conversion failed" error. Always ensure there is a logical path between the source and target types.
Another common error is forgetting to specify the length of the target type. For instance, using CAST(column AS varchar) without a length often defaults to a length of 30 characters in some contexts, which may truncate your data. Always use varchar(max) or a specific length like varchar(500) to prevent data loss.
Users also frequently try to solve this by changing the table schema. While changing a column from text to varchar(max) is a long-term solution, doing so on a production database with millions of rows can lock the table for a significant amount of time and cause application downtime. It is usually safer to handle the conversion within the query first.
Tips
When working with very large objects (LOBs), be mindful of memory usage. Casting a massive nvarchar(max) column in a query that returns thousands of rows can put a heavy load on the SQL Server buffer pool. If you only need a snippet of the data, use the LEFT() or SUBSTRING() functions in conjunction with your CAST to limit the amount of data being processed.
For better performance, create a non-clustered index on the columns you frequently filter by, but avoid indexing the columns you are casting. Casting a column in a WHERE clause (e.g., WHERE CAST(col AS varchar) = 'Value') makes the query non-sargable, meaning SQL Server cannot use an index and must perform a full table scan, which slows down performance significantly.
If you find yourself casting the same column in dozens of different queries, consider creating a Computed Column in the table. A computed column can store the casted version of the data, allowing you to select it directly without repeating the CAST logic in every single SQL statement.
Troubleshooting
If you have applied the CAST function and you are still seeing Error 421, check for nested queries or Common Table Expressions (CTEs). If a CTE selects an unsupported type, and the outer query tries to perform an operation on that result, the error may trigger at the outer level. You must apply the conversion at the lowest possible level of the query nesting.
If the error occurs within a View, remember that some View constraints prevent the use of certain LOB types. Try recreating the View and ensuring all columns are cast to compatible types within the View definition itself. If you are using a Linked Server, the error might be caused by a mismatch between the remote server's data types and the local server's interpretation of them; in this case, use OPENQUERY to perform the conversion on the remote side before the data is sent over the network.
Check for triggers or constraints. If you are seeing this error during an INSERT or UPDATE rather than a SELECT, it is likely that a trigger is attempting to select the data you just inserted into a temporary table or variable that does not support that data type. Review the trigger logic for any SELECT INTO statements.
Frequently Asked Questions
Why does this happen with 'text' and 'ntext' types?
The text and ntext types are deprecated in modern SQL Server versions. They are handled differently in memory than varchar(max) and nvarchar(max), making them incompatible with many modern T-SQL operators and selection methods.
Can I use this to fix errors in SQL Server Reporting Services (SSRS)?
Yes. SSRS often fails when a dataset contains a data type it cannot render. Casting the column to a standard string in the underlying SQL query is the standard way to resolve this.
Is CAST better than CONVERT?
CAST is ANSI-standard, meaning it is more portable across different database systems (like MySQL or PostgreSQL). CONVERT is specific to SQL Server and is more powerful for formatting dates and binary data.
Will casting a column slow down my query?
There is a slight CPU overhead for the conversion, but it is usually negligible compared to the cost of disk I/O. The main performance hit occurs if you cast a column in the WHERE clause, which prevents index usage.
What is the best replacement for the 'image' data type?
The varbinary(max) data type is the official replacement for image. It supports the same large binary storage but is compatible with more functions and selection methods.
Does this error happen in Azure SQL Database?
Yes, Azure SQL Database shares the same engine as on-premises SQL Server and will throw Error 421 under the same circumstances regarding incompatible data types.