How to get greenwich mean time in SQL Server

GETUTCDATE() is the function to get Greenwich mean time…

Just for fun…. If we want to get the same value without using this function? I have created user defined function as follows…

-- How to read Registry data into SQL Server.
select GETUTCDATE()
GO

CREATE FUNCTION dbo.fn_ConvertToUTC
    (@InDate    datetime)
RETURNS datetime
AS
BEGIN
        DECLARE @DeltaGMT int
        EXECUTE master.dbo.xp_regread 'HKEY_LOCAL_MACHINE', 
            'SYSTEM\CurrentControlSet\Control\TimeZoneInformation', 
            'ActiveTimeBias', 
            @DeltaGMT OUT 
-- Subtract to go back to the correct time
        RETURN (SELECT DATEADD(Minute, (@DeltaGMT), @InDate))
END
go


select dbo.fn_ConvertToUTC(getdate())
select GETUTCDATE()
GO

drop function fn_ConvertToUTC
GO

-Vijred

Advertisements
This entry was posted in Productivity, SQL, SQL Query and tagged , , , , , . Bookmark the permalink.

2 Responses to How to get greenwich mean time in SQL Server

  1. each time i used to read smaller articles which as well clear their motive, and that is also
    happening with this article which I am reading at this place.

  2. Hello, of course this post is actually fastidious and I have learned lot of things from it about
    blogging. thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s