Bert Wagner explains how to handle JSON datetime strings in SQL Server:
In SQL Server, datetime2’s format is defined as follows:
YYYY-MM-DD hh:mm:ss[.fractional seconds]JSON date time strings are defined like:
YYYY-MM-DDTHH:mm:ss.sssZHonestly, they look pretty similar. However, there are few key differences:
-
JSON separates the date and time portion of the string with the letter
T
-
The
Z
is optional and indicates that the datetime is in UTC (if theZ
is left off, JavaScript defaults to UTC). You can also specify a different timezone by replacing theZ
with a+
or—
along withHH:mm
(ie.-05:00
for Eastern Standard Time) -
The precision of SQL’s datetime2 goes out to 7 decimal places, in JSON and JavaScript it only goes out to 3 places, so truncation may occur.
Read on for a few scripts handling datetime conversions between these types.