According to this MSDN article:
“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.”
But when I run:
declare @d datetime;
set @d=getdate();
print CONVERT(varchar(40), @d, 127)
the result is 2012-07-31T14:04:11.447. No time zone part. What am I doing wrong?
The
DateTimetype doesn’t include a time zone, and theGETDATE()function returns a DateTime.Instead, you need to use SQL Server’s
DateTimeOffsettype, which was introduced in SQL Server 2008. That would make your example:Note the use of the
SYSDATETIMEOFFSET()function.That said, what are you actually trying to achieve, in practice? If you have some actual timezoneless data (e.g. data in a DateTime column) from which you’re trying to extract the time zone, that’s going to be tricky.