Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Strange CONVERT UTC/local strings to datetime

Author  Topic 

peteh
Starting Member

8 Posts

Posted - 2012-10-10 : 00:47:00
Why doesn't this query return two different datetime values:


select CONVERT(datetime, '2012-10-29T23:00:00Z', 127) T1, CONVERT(datetime, '2012-10-29 23:00:00', 121) T2

produces:

T1 T2
----------------------- -----------------------
2012-10-29 23:00:00.000 2012-10-29 23:00:00.000


My timezone is UTC+13 so T2 is fine but T1 should have been 2012-11-30 12:00:00.000.

BTW: This is on SQLServer 2008 EE (10.50.3720).

Any hints as to what I'm doing wrong are greatly appreciated.
Pete

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2012-10-10 : 01:46:59
SQL Server Books Online has a note "The optional time zone indicator, Z, is used to make it easier to map XML datetime values that have time zone information to SQL Server datetime values that have no time zone. Z is the indicator for time zone UTC-0. Other time zones are indicated with HH:MM offset in the + or - direction. For example: 2006-12-12T23:45:12-08:00"

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

peteh
Starting Member

8 Posts

Posted - 2012-10-10 : 02:14:26
Thanks Jack.

Incidentally, this does not even work:
select CONVERT(datetime, '2006-12-12T23:45:12-08:00', 127)
even though the documentation seems to suggest otherwise, so the only format accepted by datetime style 127 seems to be yyyy-mm-ddThh:mi:ss.mmmZ (the .mmm part being optional).

I guess the only real solution is to rewrite the query like this:
SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, GETUTCDATE(), GETDATE()), CONVERT(datetime, '2012-10-29T23:00:00Z', 127)) T1, CONVERT(datetime, '2012-10-29 23:00:00', 121) T2
but I would have thought that the SQL Server would do that implicitly when I do an ISO8601 with time zone Z conversion from a varchar to datetime.

Pete
Go to Top of Page
   

- Advertisement -