SQL Server / Editorial solution guide

Fix conversion failures in dates and typed parameters

Locate the actual conversion boundary, preserve Unicode input, and repair type contracts without silently accepting invalid business data.

About 3 min readContent reviewed

Read-only knowledge base ยท No public comments

Recognize the symptoms

Messages can include Conversion failed when converting date and/or time from character string. or Error converting data type nvarchar to numeric. Preserve the exact observed message and error number in diagnostics.

The same query may behave differently under another language/date-format setting, or fail only for one input containing a decimal separator, an ambiguous date, or a value outside the target range.

Understand the likely causes

  • Text is being interpreted as a typed value at a driver parameter, expression, comparison, assignment, or destination column. Implicit conversions follow SQL Server type-precedence rules and may occur somewhere other than the projection you first inspect.
  • Unicode loss and conversion failure are related input-contract concerns but are not the same symptom. A missing N prefix or non-Unicode parameter can damage text before it reaches an nvarchar destination.

Trace types across the whole boundary

Record the source format, driver parameter type and length, stored procedure signature, intermediate variables, and target column type, precision, and scale. Use synthetic reproductions instead of pasting personal or financial input into a public issue. Do not assume a wider destination repairs text already truncated in an intermediate variable. Inspect implicit conversion warnings and expression types when the error seems unrelated to the displayed column.

Identify invalid text without changing rows

TRY_CONVERT returns NULL for a supported conversion that fails, allowing a read-only investigation of known-format text. It can still error for explicitly disallowed conversions. The example uses style 112 for an agreed eight-digit date and decimal parsing for a simple numeric contract. It distinguishes malformed input from valid typed values; a NULL source and an invalid source still need separate classification in real imports.

sql Read-only conversion diagnosis
SELECT RawDate,TRY_CONVERT(date,RawDate,112) AS parsed_date,
 RawAmount,TRY_CONVERT(decimal(10,2),RawAmount) AS parsed_amount
FROM (VALUES (N'20260131',N'19.95'),
 (N'20260230',N'not-a-number'))v(RawDate,RawAmount);
Expected output
RawDate  parsed_date RawAmount    parsed_amount
20260131 2026-01-31  19.95        19.95
20260230 NULL        not-a-number NULL

February 30 is invalid. The failed numeric conversion does not silently become zero. Row display order is not guaranteed without ORDER BY.

Repair the input contract at its source

Prefer a driver date parameter for a date and an explicitly sized decimal parameter for an amount. If importing text, agree an invariant format and an explicit conversion style, then quarantine rejected rows with reasons. Do not change the whole server's language to accommodate one file. Do not coalesce failed conversions to zero or a sentinel date unless that transformation is an explicit business policy.

Preserve Unicode through every stage

Use nvarchar parameters and columns for the course's multilingual contract and N-prefixed Unicode literals in T-SQL. Length n measures UTF-16 byte-pairs, so supplementary characters need careful boundary testing. SQL Server 2019 UTF-8 varchar is an explicit collation alternative, not a property of every varchar. Bind values rather than concatenating them into dynamic SQL; parameterization also prevents apostrophes from becoming command syntax.

Verify the fix

Verify known valid dates, leap-day cases, invalid dates, NULL, decimal limits, Arabic names, apostrophes, and maximum-length values through the actual application binding path. Confirm rejected inputs remain rejected with a clear response, valid values round-trip unchanged, and comparison predicates no longer convert indexed columns unnecessarily.

Keep it from recurring

  • Version type signatures with the API and schema. Keep import formats, timezone conventions, numeric scales, and Unicode lengths documented. Test culture differences at the boundary, and retain failed-row evidence without exposing sensitive original data publicly.

Official references