How to compare datetime2 to datetime
I need to compare the datetime2 property of that one database with the datetime property name that the other database, because I don't find a method to convert datetime to datetime2.
I am testing this sentence:
select CAST(FechaAlta AS datetime) from tutors
but return this error message:
Converting the datetime2 data type to the datetime data type resulted in an out-of-level value.
source to share
The datatype datetime2
has a wider range than datetime
- datetime2
can contain any date between 0001-01-01
and 9999-12-31
, whereas it datetime
starts with 01/01/1753
(there are unclear reasons for this involving the 4th Earl of Chesterfield, which are too mysterious to enter at this point).
If you want to convert your datetime2 to datetime you need to handle out-of-range values ββlike:
SELECT CASE WHEN FechaAlta < '1753-01-01' THEN NULL ELSE CAST(FechaAlta AS DateTime) END AS FechaAlta_As_DateTime FROM Tutors
source to share