[Company Logo Image] 

Home Up Contents Coffee Break Credits Glossary Links Search

 
Conversion resulted in an out-of-range value

 

 

Home
Analysis Services
Azure
CLR Integration
High Availability
Open Source
Security
SQL Server 2008
SQL Server 2012
SQL Server 2014
SQL Server 2016
SQL Server 2017
SQL Server 2019
Tips
Troubleshooting
Tuning

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.


Applies to: Microsoft SQL Server 2008 and above.
 

Problem Description.
 

Using SQL Server Management Studio, we created the following piece of T-SQL code and execute it.
 

DECLARE @DATETIME datetime = '1700-01-01'

SELECT @DATETIME

 

We received the following error message:


Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.


(1 row(s) affected)


Cause.


The datetime data type does not accept dates earlier than January 1, 1753.


Solution.

To solve this problem, you can use the new "date" data type introduced on SQL Server 2008.

For example:
 

DECLARE @DATETIME date = '1752-12-31'

SELECT @DATETIME

DECLARE @DATETIME_B date = '0001-01-01'

SELECT @DATETIME_B


The results are:


----------
1752-12-31

(1 row(s) affected)


----------
0001-01-01

(1 row(s) affected)

 


 

 

.Send mail to webmaster@sqlcoffee.com with questions or comments about this web site.