SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Strange CONVERT UTC/local strings to datetime
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

peteh
Starting Member

8 Posts

Posted - 10/10/2012 :  00:47:00  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

United Kingdom
2058 Posts

Posted - 10/10/2012 :  01:46:59  Show Profile  Visit jackv's Homepage  Reply with Quote
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 - 10/10/2012 :  02:14:26  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000