Out-of-Range DATETIME Values in SQL Server Transact-SQL

Q

What Are Out-of-Range Errors with DATETIME values in SQL Server Transact-SQL?

✍: FYIcenter.com

A

When you enter DATETIME values, you may get out-of-range errors due to two common mistakes:

  • The date value is a valid calendar date, but it is not in the range covered by DATETIME data type: from January 1, 1753, to December 31, 9999.
  • The date value is not a valid calendar date, for example: 30-Feb-2017.
  • The time value does not represent a valid time, for example: 24:55:07.233.

The tutorial exercise below shows you some data and time out-of-range errors:

-- Invalid date
DECLARE @x DATETIME;
SET @x = '30-Feb-2017 22:55:07.233';
-----------------------------------
Msg 242, Level 16, State 3, Line 2
The conversion of a char data type to a datetime data type
   resulted in an out-of-range datetime value.

-- Date below the DATETIME limits
DECLARE @x DATETIME;
SET @x = '19-May-1752 22:55:07.233';
-----------------------------------
Msg 242, Level 16, State 3, Line 2
The conversion of a char data type to a datetime data type
   resulted in an out-of-range datetime value.

-- Invalid time
DECLARE @x DATETIME;
SET @x = '19-May-2017 24:55:07.233';
-----------------------------------
Msg 242, Level 16, State 3, Line 2
The conversion of a char data type to a datetime data type
   resulted in an out-of-range datetime value.

 

Variables and Data Types in SQL Server Transact-SQL

⇒⇒SQL Server Transact-SQL Tutorials

2017-04-08, 292👍, 0💬