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.
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!
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.
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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With