SYSDATETIMEOFFSET precision decreased on Azure SQL

%3CLINGO-SUB%20id%3D%22lingo-sub-2669089%22%20slang%3D%22en-US%22%3ESYSDATETIMEOFFSET%20precision%20decreased%20on%20Azure%20SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2669089%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22uvIdeaDescription%20typeset%22%3E%3CDIV%20class%3D%22typeset%22%3E%3CP%3EOriginally%20posted%20on%20stackexchange%20(%3CA%20href%3D%22https%3A%2F%2Fdba.stackexchange.com%2Fquestions%2F287688%2Fwhy-is-sysdatetimeoffset-precision-decreased-on-azure-sql%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdba.stackexchange.com%2Fquestions%2F287688%2Fwhy-is-sysdatetimeoffset-precision-decreased-on-azure-sql%3C%2FA%3E)%2C%20then%20on%20feedback.azure.com%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ffeedback.azure.com%2Fforums%2F908035-sql-server%2Fsuggestions%2F43034130-sysdatetimeoffset-precision-decreased-on-azure-sql%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E(https%3A%2F%2Ffeedback.azure.com%2Fforums%2F908035-sql-server%2Fsuggestions%2F43034130-sysdatetimeoffset-precision-decreased-on-azure-sql%3C%2FA%3E)%20until%20that%20was%20shut%20down%2C%20now%20here.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESELECT%20SYSDATETIMEOFFSET()%3C%2FP%3E%3CP%3EWhen%20I%20run%20the%20query%20on%20a%20local%20SQL%20Server%202019%20instance%2C%20I%20get%20values%20like%20this%3A%3C%2FP%3E%3CP%3E2021-03-24%2014%3A32%3A32.5811589%20-05%3A00%3CBR%20%2F%3E2021-03-24%2014%3A32%3A37.8169216%20-05%3A00%3CBR%20%2F%3E2021-03-24%2014%3A32%3A42.6465055%20-05%3A00%3C%2FP%3E%3CP%3EWhen%20I%20run%20the%20query%20on%20an%20Azure%20SQL%20database%2C%20I%20get%20values%20like%20this%3A%3C%2FP%3E%3CP%3E2021-03-24%2019%3A31%3A04.6733333%20%2B00%3A00%3CBR%20%2F%3E2021-03-24%2019%3A31%3A15.8300000%20%2B00%3A00%3CBR%20%2F%3E2021-03-24%2019%3A31%3A20.4866666%20%2B00%3A00%3C%2FP%3E%3CP%3EIn%20the%20latter%2C%20the%20last%205%20digits%20are%20always%20repeating%20values.%20The%20value%20is%20less%20precise%20(or%20do%20I%20mean%20less%20accurate%3F)%20when%20the%20function%20runs%20on%20Azure%20SQL.%20Why%20is%20the%20behavior%20different%3F%3C%2FP%3E%3CP%3EThe%20documentation%20for%20SYSDATETIMEOFFSET%20says%3A%3C%2FP%3E%3CPRE%3ESQL%20Server%20obtains%20the%20date%20and%20time%20values%20by%20using%20the%20GetSystemTimeAsFileTime()%20Windows%20API.%20The%20accuracy%20depends%20on%20the%20computer%20hardware%20and%20version%20of%20Windows%20on%20which%20the%20instance%20of%20SQL%20Server%20is%20running.%20The%20precision%20of%20this%20API%20is%20fixed%20at%20100%20nanoseconds.%20The%20accuracy%20can%20be%20determined%20by%20using%20the%20GetSystemTimeAdjustment()%20Windows%20API.%3C%2FPRE%3E%3CP%3EBut%20if%20the%20hardware%20matters%2C%20I%20don't%20expect%20I'll%20be%20able%20to%20know%20that%20information%20for%20Azure%20SQL.%3C%2FP%3E%3CP%3EI%20am%20working%20around%20this%20issue%20by%20instead%20using%20this%20sort%20of%20query%20to%20get%20a%20more%20precise%20datetimeoffset%20value%3A%3C%2FP%3E%3CP%3ESELECT%20(SYSUTCDATETIME()%20AT%20TIME%20ZONE%20'UTC')%3C%2FP%3E%3CP%3EFor%20some%20reason%20SYSUTCDATETIME%20doesn't%20suffer%20from%20the%20%22last%205%20digits%20repeat%22%20issue%20when%20I%20run%20it%20on%20Azure%20SQL.%20The%20documentation%20for%20SYSUTCDATETIME%20does%20say%3A%3C%2FP%3E%3CPRE%3ESYSDATETIME%20and%20SYSUTCDATETIME%20have%20more%20fractional%20seconds%20precision%20than%20GETDATE%20and%20GETUTCDATE.%3C%2FPRE%3E%3CP%3Ebut%20it%20doesn't%20mention%20having%20more%20precision%20than%20SYSDATETIMEOFFSET.%3C%2FP%3E%3CP%3EEither%20fix%20SYSDATETIMEOFFSET%20on%20Azure%20SQL%20or%20expand%20the%20documentation%20so%20that%20developers%20can%20know%20what%20precision%20can%20be%20relied%20upon%20from%20SYSDATETIMEOFFSET%20and%20SYSUTCDATETIME%20on%20Azure%20SQL.%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
New Contributor

Originally posted on stackexchange (https://dba.stackexchange.com/questions/287688/why-is-sysdatetimeoffset-precision-decreased-on-azure...), then on feedback.azure.com

(https://feedback.azure.com/forums/908035-sql-server/suggestions/43034130-sysdatetimeoffset-precisio...) until that was shut down, now here.

 

SELECT SYSDATETIMEOFFSET()

When I run the query on a local SQL Server 2019 instance, I get values like this:

2021-03-24 14:32:32.5811589 -05:00
2021-03-24 14:32:37.8169216 -05:00
2021-03-24 14:32:42.6465055 -05:00

When I run the query on an Azure SQL database, I get values like this:

2021-03-24 19:31:04.6733333 +00:00
2021-03-24 19:31:15.8300000 +00:00
2021-03-24 19:31:20.4866666 +00:00

In the latter, the last 5 digits are always repeating values. The value is less precise (or do I mean less accurate?) when the function runs on Azure SQL. Why is the behavior different?

The documentation for SYSDATETIMEOFFSET says:

SQL Server obtains the date and time values by using the GetSystemTimeAsFileTime() Windows API. The accuracy depends on the computer hardware and version of Windows on which the instance of SQL Server is running. The precision of this API is fixed at 100 nanoseconds. The accuracy can be determined by using the GetSystemTimeAdjustment() Windows API.

But if the hardware matters, I don't expect I'll be able to know that information for Azure SQL.

I am working around this issue by instead using this sort of query to get a more precise datetimeoffset value:

SELECT (SYSUTCDATETIME() AT TIME ZONE 'UTC')

For some reason SYSUTCDATETIME doesn't suffer from the "last 5 digits repeat" issue when I run it on Azure SQL. The documentation for SYSUTCDATETIME does say:

SYSDATETIME and SYSUTCDATETIME have more fractional seconds precision than GETDATE and GETUTCDATE.

but it doesn't mention having more precision than SYSDATETIMEOFFSET.

Either fix SYSDATETIMEOFFSET on Azure SQL or expand the documentation so that developers can know what precision can be relied upon from SYSDATETIMEOFFSET and SYSUTCDATETIME on Azure SQL.

0 Replies