Getting the current date and time in SQL Server, and using timezones with DateTimeOffset

preview_player
Показать описание
In this video, we will be looking at the various functions and the differences between them to getting the current time, and using DateTimeOffset.
My SQL Server Udemy courses are:
----
My Excel for PC courses are:
----
In this video, we will be looking at the following functions, which return s a datetime, accurate to a 1/300th of a second:
CURRENT_TIMESTAMP - the ANSI SQL version.
GETDATE() - this is the SQL Server version of CURRENT_TIMESTAMP.
GETUTCDATE() - this returns the GETDATE() for UTC/GMT.

The following returns a datetime2(7), which returns up to 7 decimal places.
SYSDATETIME() - this is the equivalent of GETDATE()
SYSUTCDATETIME() - this is the equivalent of GETUTCDATE()

In addition, we will be looking at:
SYSDATETIMEOFFSET() - this returns a datetimeoffset, so it includes a timezone.
CURRENT_TIMEZONE() - this returns the timezone (in winter)
DATETIMEOFFSETFROMPARTS - this takes 10 numerical parameters, for year, month, day, hour, minute, second, fraction of a second, hour offset, minute offset, and decimal places for the fraction of a second.
TODATETIMEOFFSET - this converts a datetime to a datetimeoffset.
SWITCHOFFSET - this converts a datetimeoffset in one timezone to another timezone.
TODATETIMEOFFSET(CURRENT_TIMESTAMP, '-07:00')
SWITCHOFFSET(SYSDATETIMEOFFSET(), '-07:00')
Рекомендации по теме
Комментарии
Автор

I also live in UK, I’m facing lot of issues using the in my synapse datawarehouse, I felt you explained it well but still the confusion with UTC timezone is not clear for me. Which function out of these gives me the correct time whether its day light savings or normal?

MegaVivek
Автор

How to show database stored date time to user's current timezone

Mr--View
Автор

Hello, I have a problem with my timezones in MS SQL. My sysdatetime is set correctly, but when I fetch the data, it seems that I get the time from sysutcdatetime, which is -2 hours. I tried to change it with switchoffset but I get an error: the timezone provided to builtin function switchoffset is invalid.
Could you help me, please?

Daniel-ytls
Автор

Hi Phillip, Thanks for your great efforts in making such tutorials, can we user this query so that we can get all the Date and Time functions in a single row.

select CURRENT_TIMESTAMP 'CURRENT_TIMESTAMP',
GETDATE() 'GETDATE',
GETUTCDATE() 'GETUTCDATE',
SYSDATETIME() 'SYSDATETIME',
SYSUTCDATETIME()'SYSUTCDATETIME',
SYSDATETIMEOFFSET() 'SYSDATETIMEOFFSET',
CURRENT_TIMEZONE() 'CURRENT_TIMEZONE',
DATETIMEOFFSETFROMPARTS(2025, 02, 03, 14, 25, 35, 123, 1, 0, 3) 'DATETIMEOFFSETFROMPARTS';

pavan