Click here to Skip to main content
15,886,026 members
Articles / Database Development / SQL Server

SQL Server - Convert UTC to Local Time

Rate me:
Please Sign up or sign in to vote.
4.82/5 (15 votes)
11 Jul 2006CPOL 188.6K   2.9K   31   28
Convert UTC to local time

Introduction

SQL Server does not provide a simple way to convert a UTC datetime value to a local time value. This project includes a table (tbTimeZoneInfo) with data to provide the Time Zone information and two functions to convert a UTC datetime value to any Local Time Zone.

The tbTimeZoneInfo table contains the Time Zone information for all the time zones from the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones. Anyone familiar with the TIME_ZONE_INFORMATION structure will recognize the structure of table tbTimeZoneInfo.

The function named GetLocalDateTime arguments are a TimeZoneID value from tbTimeZoneInfo and a UTC DateTime value. The GetUTCDate() function can be used to obtain the current UTC date.

Values in the tbTimeZoneInfo can be changed as needed. The values that are supplied in the form of SQL insertion scripts may not be up to date. They were obtained from the registry at a point in time and may now be out of date.

Simply read the UTCToLocal_ReadMe.txt file for instructions on creating the objects, inserting data and testing the GetLocalDateTime function.

History

  • 11th July, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Live in Santa Barbara, CA aka "Paradise" with my awesome wife and daughter. Work as a software developer/typist.

Comments and Discussions

 
GeneralResponse to question about returning local date only Pin
Chris Tillotson24-Jul-06 10:10
Chris Tillotson24-Jul-06 10:10 
GeneralThis is great!! One question Pin
mingo336924-Jul-06 8:25
mingo336924-Jul-06 8:25 
GeneralLink to file is broken. Pin
Marc Leger11-Jul-06 16:04
Marc Leger11-Jul-06 16:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.