I have a column abc varchar(100)
with data like 2011-09-26 16:36:57.810000
I want to convert this column to DATETIME
...
Assuming we have the following string variables:
DECLARE @d VARCHAR(100) = '2020-04-06T04:35:07.9490051Z' -- 7 digits nanoseconds
DECLARE @d1 VARCHAR(100) = '2020-04-05T15:00:00Z' -- simple: without nanoseconds
I came up to the solution using CAST operator:
SELECT CAST(LEFT(@d,19) + 'Z' AS DATETIME) -- outputs: 2020-04-06 04:35:07.000
SELECT CAST(LEFT(@d1,19) + 'Z' AS DATETIME) -- outputs: 2020-04-05 15:00:00.000