SQL Server converts data from one type to another in two ways. An implicit conversion happens on its own when two different data types meet in an expression, an assignment or a comparison. An explicit conversion is one you write yourself, with CAST or CONVERT. Both give the same answer most of the time, but an implicit conversion on a column in a WHERE clause can turn an index seek into a scan, and that is where most of the trouble comes from.
What is Data Conversion?
Data conversion transforms a value from one data type to another. SQL Server does it in two ways:
Implicit Conversion
> Happens automatically when the two data types are compatible. SQL Server converts the type with the lower data type precedence to the higher one.
> Example: 1 + '1' returns 2, because INT ranks above VARCHAR, so the string is converted to a number.
Explicit Conversion
> Uses CAST and CONVERT, or TRY_CAST and TRY_CONVERT.
> Required where no implicit conversion exists. A DATETIME won’t go into an INT on its own, but CAST converts it.
Implicit doesn’t mean safe. These all run without a CAST in sight, and only the first one does what you might expect:
SELECT 1 + '1' AS Result; -- 2 SELECT 1 + 'a'; -- Msg 245, Conversion failed when converting the varchar value 'a' to data type int. DECLARE @i INT = 9.99; SELECT @i AS DecimalIntoInt; -- 9, the decimals are dropped DECLARE @v VARCHAR(5) = 'abcdefgh'; SELECT @v AS Truncated; -- abcde, no error DECLARE @dt DATETIME = '2025-01-25'; DECLARE @n INT = @dt; -- Msg 257, Implicit conversion from data type datetime to int is not allowed. SELECT CAST(@dt AS INT) AS DaysSince1900; -- 45680, the explicit version works
Error 245 is the one you’ll meet most in real code, usually from a VARCHAR column holding a stray non-numeric value. Conversion Failed When Converting the varchar Value to Data Type int (Error 245) on sqldba.blog shows how to find the row.
SQL Server Data Type Conversion Chart
Microsoft’s conversion chart shows, for every pair of data types, whether the conversion is implicit, explicit only, or not allowed at all:

For the most accurate and updated version, refer to the official SQL Server Data Type Conversion Documentation.
Interpreting the Conversion Chart
> INT to FLOAT is implicit, and happens without you asking.
> VARCHAR to DATETIME is implicit too, so DECLARE @d DATETIME = '2025-01-25'; works. It only fails when SQL Server can’t read the string as a date.
> DATETIME to INT is explicit only (error 257 above), and DATE to INT isn’t allowed at all (error 206, Operand type clash).
Data Type Precedence: Which Side Gets Converted
When two types meet, SQL Server never asks which side it should convert. It converts the lower-precedence side, every time. Compare a VARCHAR column with an INT value, and it’s the column that gets converted, on every row. Compare a VARCHAR column with an NVARCHAR value, the type .NET sends strings as by default, and again it’s the column.
A few of the common types, highest first: DATETIME2, DATETIME, DATE, FLOAT, DECIMAL, MONEY, BIGINT, INT, BIT, NVARCHAR, NCHAR, VARCHAR, CHAR. Character types sit near the bottom, which is why a string column is nearly always the side that loses.
Implicit Conversions in Execution Plans
Converting the column means SQL Server can’t seek on that column’s index, because every row has to be converted before it can be compared. Here’s a test table with a VARCHAR order reference and an index on it:
CREATE TABLE dbo.Orders (
OrderID INT IDENTITY PRIMARY KEY,
OrderRef VARCHAR(10) NOT NULL,
Filler CHAR(200) NOT NULL DEFAULT ''
);
INSERT dbo.Orders (OrderRef)
SELECT TOP (100000) CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS VARCHAR(10))
FROM sys.all_objects AS a
CROSS JOIN sys.all_objects AS b;
CREATE INDEX IX_OrderRef ON dbo.Orders (OrderRef);Now the same lookup three ways, with the actual execution plan on (Ctrl+M in SSMS):
SET STATISTICS IO ON; SELECT OrderID FROM dbo.Orders WHERE OrderRef = 12345; -- INT: Index Scan, 236 logical reads SELECT OrderID FROM dbo.Orders WHERE OrderRef = N'12345'; -- NVARCHAR: Index Scan, 236 logical reads SELECT OrderID FROM dbo.Orders WHERE OrderRef = '12345'; -- VARCHAR: Index Seek, 2 logical reads
Tested on SQL Server 2025, all three return the same row. The first two scan the whole index, and the plan shows CONVERT_IMPLICIT wrapped around OrderRef. The SELECT operator also carries a yellow warning triangle: Type conversion in expression (CONVERT_IMPLICIT(…)) may affect “SeekPlan” in query plan choice. The third query matches the column’s type and seeks straight to the row.
The NVARCHAR case depends on collation. The test database uses the SQL Server default SQL_Latin1_General_CP1_CI_AS, which scans. With a Windows collation such as Latin1_General_CI_AS, SQL Server can still seek using a range, though the conversion still shows in the plan.
On 100,000 rows the difference is 236 reads against 2. On a busy table with millions of rows it’s the difference between a query that runs in milliseconds and one that drives CPU up. How CONVERT_IMPLICIT Turns a Seek Into a Scan on sqldba.blog goes further into reading these plans, and DBA Scripts: Get Implicit Conversions finds the queries in the plan cache that already have this problem.
Not every CONVERT_IMPLICIT is a problem. Run WHERE OrderID = '12345' against the INT key and the plan shows CONVERT_IMPLICIT(int, ...) on the value, not the column, and still seeks. It’s a conversion on the column side that costs you.
Examples of Explicit Conversions
Explicit conversions give you control over how the value is converted, and they make the intent obvious to whoever reads the code next. Here are practical examples:
Valid Explicit Conversion:
Converting a VARCHAR containing a valid date to a DATETIME:
DECLARE @dateString VARCHAR(10) = '2025-01-25'; SELECT CAST(@dateString AS DATETIME) AS ConvertedDate; SELECT CONVERT(DATETIME, @dateString, 120) AS ConvertedDateWithStyle;
In the above example:
> CAST converts the VARCHAR to DATETIME directly.
> CONVERT takes a style code that says how to read the string. Style 120 is ODBC canonical, yyyy-mm-dd hh:mi:ss.
Why the Style Code Matters
Both lines return 2025-01-25 00:00:00.000 on a US English login. Switch the session to British English and the CAST fails, because DATETIME reads yyyy-mm-dd using the session’s date format, and British is day-month. The style code doesn’t care about the language:
SET LANGUAGE British;
DECLARE @dateString VARCHAR(10) = '2025-01-25';
SELECT CONVERT(DATETIME, @dateString, 120) AS WithStyle; -- 2025-01-25 00:00:00.000
SELECT CAST('20250125' AS DATETIME) AS NoDashes; -- 2025-01-25 00:00:00.000
SELECT CAST(@dateString AS DATETIME) AS PlainCast; -- Msg 242, ... resulted in an out-of-range value.
SET LANGUAGE us_english;yyyymmdd with no dashes is read the same way under every language setting, and so is yyyy-mm-dd going into DATE or DATETIME2. Only the old DATETIME and SMALLDATETIME types trip on it.
Explicit Conversion with Errors
When the VARCHAR isn’t a recognisable date, the conversion fails:
DECLARE @invalidDate VARCHAR(10) = 'invalid'; SELECT CAST(@invalidDate AS DATETIME); -- Results in an error /* Msg 241, Level 16, State 1, Line 2 Conversion failed when converting date and/or time from character string. */
Best Practices for Data Conversion
Match the Column’s Data Type:
Declare variables and parameters with the same type and length as the column they’re compared with. That alone prevents most implicit conversion scans. In application code, that means setting the parameter type rather than letting the driver send every string as NVARCHAR.
Validate Input:
Ensure input data is clean and adheres to expected formats before conversion.
Use Explicit Conversion for Clarity:
Even when implicit conversion is possible, explicit conversion improves code readability and reduces ambiguity. Convert the value, never the column.
Handle Errors Gracefully:
Use TRY_CONVERT or TRY_CAST to avoid runtime errors in case of invalid data:
DECLARE @invalidDate VARCHAR(10) = 'invalid'; SELECT TRY_CAST(@invalidDate AS DATETIME) AS SafeConversion;
This returns NULL instead of throwing an error, which also makes it a quick way to find the bad rows: WHERE TRY_CAST(col AS DATETIME) IS NULL AND col IS NOT NULL.
Leave a Reply