Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert string timestamp in format yyyy-MM-ddTHH:mm:ss.ffffffZ to SQL Server datetime without changing time zone

I would like to transform a UTC timestamp with 5-6 ms digits into a SQL Server datetime. I do not want to localize or anything, just want to keep UTC but turn it into a datetime

e.g.

declare @utc_timestamp varchar(20) = '2022-10-05T13:12:02.000402Z';

-- Desired datetime format (is this even possible or does this not conform to SQL Server's datetime format?
2022-10-05 13:12:02.000402

Interestingly, this works:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

declare @utc_timestamp varchar(20) = '2022-10-05T13:12:22Z';
select cast(@utc_timestamp as datetime)

-- result
2022-10-05 13:12:22.000

So it leads me to believe that SQL Server’s datetime can only parse a UTC timestamp if it doesn’t have too many ms digits.

>Solution :

You can use datetime2 for this, it has a higher precision.

select cast('2022-10-05T13:12:02.000402Z' as datetime2)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading