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.

+3


source to share


2 answers


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

      

+2


source


First get datetime2 and datetime in the same format datetime

. Assuming they both matter date datatype

and you are working onOracle



TO_DATE(TO_CHAR(DATETIME2,'YYYYMMDD HH24:MI:SS')) = TO_DATE(TO_CHAR(DATETIME,'YYYYMMDD HH24:MI:SS')) 

      

-1


source







All Articles