# SQL Server Msg 242: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

**Code:** `242` · **Category:** SQL · **Seen on:** SQL Server, SQL

A date string couldn't be converted because it's invalid or in the wrong format for the session language.

## Meaning

Use ISO `YYYYMMDD` or `YYYY-MM-DDTHH:MM:SS`, parameterise dates as `datetime2`, and avoid depending on `SET DATEFORMAT`/language. `datetime` ranges from 1753; `datetime2` from 0001.

## Common causes

- dd/mm/yyyy read as mm/dd/yyyy
- Invalid date such as 31 April
- Date before 1753 for datetime
- Empty string converted to date
- Wrong DATEFORMAT/language

## Quick fix

1. Use ISO 8601 / `YYYYMMDD` literals
2. Use `datetime2` and typed parameters
3. Validate with `TRY_CONVERT`

## Detailed fix by platform

### SQL

1. `SELECT TRY_CONVERT(datetime2, '2026-13-45') AS ok_or_null;`

## How to diagnose

1. **Format** — ISO?
2. **Type** — datetime2?
3. **Value** — Valid date?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/sql/mssql-conversion-date-datetime-out-of-range-242)
