I have been using this format for years to truncate dates and times
SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, '1980-02-05 12:45'), 0) AS Hour ,
DATEADD(DAY, DATEDIFF(DAY, 0, '1980-02-05 12:45'), 0) AS Day ,
DATEADD(MONTH, DATEDIFF(MONTH, 0, '1980-02-05 12:45'), 0) AS Month ,
DATEADD(YEAR, DATEDIFF(YEAR, 0, '1980-02-05 12:45'), 0) AS Year;
But I have a need to store very early dates like 1400-01-01
and therefore I can use DateTime2
.
But how would I support the ability to still truncate like above using DateTime2
?
Changing the year above to 1400
from 1980
will then result in
SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, '1400-02-05 12:45'), 0) AS Hour ,
DATEADD(DAY, DATEDIFF(DAY, 0, '1400-02-05 12:45'), 0) AS Day ,
DATEADD(MONTH, DATEDIFF(MONTH, 0, '1400-02-05 12:45'), 0) AS Month ,
DATEADD(YEAR, DATEDIFF(YEAR, 0, '1400-02-05 12:45'), 0) AS Year;
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
SO casting to DateTime2
SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, CAST('1400-02-05 12:45' AS DATETIME2)),0) AS Hour ,
DATEADD(DAY, DATEDIFF(DAY, 0, CAST('1400-02-05 12:45' AS DATETIME2)),0) AS Day ,
DATEADD(MONTH, DATEDIFF(MONTH, 0, CAST('1400-02-05 12:45' AS DATETIME2)), 0) AS Month ,
DATEADD(YEAR, DATEDIFF(YEAR, 0, CAST('1400-02-05 12:45' AS DATETIME2)), 0) AS Year;
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
I am assuming that the 0
is being treated as a DateTime
data type and effectly casting it to DateTime
.
Trying to cast the 0
to DateTime2
using CAST(0 AS DATETIME2)
gives me this error
Explicit conversion from data type int to datetime2 is not allowed.
In the end I am wanting to use these as persisted columns in a table which worked fine with DateTime
data types but not so easy with DateTime2