SQL Server UTC Date Functions
Ran across some SQL statements that were using the expression SYSUTCDATETIME()
which is a statement I have not used and wanted to know the difference between this and my common go to statement of GETUTCDATE()
Test
select GETUTCDATE(), SYSUTCDATETIME()
Output
(No column name) (No column name)
2019-06-17 16:52:18.693 2019-06-17 16:52:18.6947071
Looks the same to me
More detailed Test
Time to use the SQL_VARIANT_PROPERTY() function to obtain more details
select SQL_VARIANT_PROPERTY(GETUTCDATE(), 'BaseType') as DataType,SQL_VARIANT_PROPERTY(GETUTCDATE(), 'Precision') as PrecisionDetail,SQL_VARIANT_PROPERTY(GETUTCDATE(), 'Scale') as Scale
select SQL_VARIANT_PROPERTY(SYSUTCDATETIME(), 'BaseType') as DataType,SQL_VARIANT_PROPERTY(SYSUTCDATETIME(), 'Precision') as PrecisionDetail,SQL_VARIANT_PROPERTY(SYSUTCDATETIME(), 'Scale') as Scale
Output
DataType PrecisionDetail Scale
datetime 23 3
DataType PrecisionDetail Scale
datetime2 27 7
Turns out the more accurate variant is the SYSUTCDATETIME()
, therefore going forward I need to use this when dealing with SQL Date times unless otherwise informed not to use that format