Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range?

I am working on application contains a datepicker and if I set the time in that picker to a very old value or far in the future when I try to save this value in the database the server throw this exception, what is the cause of it?

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.

like image 693
Geronimo18 Avatar asked Sep 12 '11 10:09

Geronimo18


People also ask

How do you solve the conversion of a DateTime2 data type to a DateTime data type resulted in an out of range value?

Short Answer This can happen if you do not initialize a value to a DateTime field; the field does not accept NULL values, and it's a value type, so the default value of the non-nullable DateTime type will be used. Setting the value fixed it for me!

What is DateTime2 data type in SQL Server?

Defines a date that is combined with a time of day that is based on 24-hour clock. datetime2 can be considered as an extension of the existing datetime type that has a larger date range, a larger default fractional precision, and optional user-specified precision.

What's the difference between DateTime and DateTime2?

The main difference is the way of data storage: while in Datetime type, the date comes first and then time, in Datetime2, 3 bytes, in the end, represents date part!

What is a DateTime2 data type?

The DateTime2 is an SQL Server data type, that stores both date & time together. The time is based on the 24 hours clock. The DateTime2 stores the fractional seconds Up to 7 decimal places (1⁄10000000 of a second). The Precision is optional and you can specify it while defining the DateTime2 column.


1 Answers

DateTime has the range: January 1, 1753, through December 31, 9999

DateTime2 has the range: 0001-01-01 through 9999-12-31

So if you are entering a date before 1753 you would get this error when the field in the table is of type DateTime.

like image 85
AnthonyWJones Avatar answered Oct 05 '22 08:10

AnthonyWJones