Lynchie
Lynchie

Reputation: 1149

SQL Convert yyyy-mm-ddTHH:mm:ss to DateTime

How can I convert

2016-01-20T14:27:36.270239Z

To a SQL DateTime?

I have tried

CAST('2016-01-20T14:27:36.270239Z' AS datetime)

But receive a conversion error.

Cheers

Upvotes: 5

Views: 13122

Answers (2)

Moe Sisko
Moe Sisko

Reputation: 12051

Note that DATETIME is not precise enough to represent the value you are trying to store.

If you don't need to store the the value exactly as is, Kamran's answer may suit.

If you need the full precision, use a DATETIME2. e.g. :

SELECT CONVERT(Datetime2,'2016-01-20T14:27:36.270239Z',127) 

Upvotes: 1

Fuzzy
Fuzzy

Reputation: 3810

You can do this:

SELECT CAST(CONVERT(datetimeoffset,'2016-01-20T14:27:36.270239Z',127) AS DATETIME)

Upvotes: 4

Related Questions