Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8105031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:03:54+00:00 2026-06-06T00:03:54+00:00

I have a few columns in an SQL server 2008 R2 database that i

  • 0

I have a few columns in an SQL server 2008 R2 database that i need to convert from local time (the time zone the sql server is in) to UTC.

I have seen quite a few similar questions on StackOverflow, but the answers all fail to work correctly with daylight saving time, they only take into account the current difference and offset the date.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:03:54+00:00Added an answer on June 6, 2026 at 12:03 am

    I could not find any way at all doing this using T-SQL alone. I solved it using SQL CLR:

    public static class DateTimeFunctions
    {
        [SqlFunction(IsDeterministic = true, IsPrecise = true)]
        public static DateTime? ToLocalTime(DateTime? dateTime)
        {
            if (dateTime == null) return null;
            return dateTime.Value.ToLocalTime();
        }
    
        [SqlFunction(IsDeterministic = true, IsPrecise = true)]
        public static DateTime? ToUniversalTime(DateTime? dateTime)
        {
            if (dateTime == null) return null;
            return dateTime.Value.ToUniversalTime();
        }
    }
    

    And the following registration script:

    CREATE FUNCTION ToLocalTime(@dateTime DATETIME2) RETURNS DATETIME2 AS EXTERNAL NAME AssemblyName.[AssemblyName.DateTimeFunctions].ToLocalTime; 
    GO
    CREATE FUNCTION ToUniversalTime(@dateTime DATETIME2) RETURNS DATETIME2 AS EXTERNAL NAME AssemblyName.[AssemblyName.DateTimeFunctions].ToUniversalTime; 
    

    It is a shame to be forced to go to such effort to convert to and from UTC time.

    Note, that these functions interpret local time as whatever is local to the server. It is recommended to have clients and servers set to the same time zone to avoid any confusion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Sql-Server-2008 database that I am querying from on the regular that
We have a few tables in our SQL Server (2005 & 2008) database with
I have a database in SQL Server 2008 that supports various applications. I am
We have a few tables with persisted computed columns in SQL Server. Is there
I have a few windwos services. They get xml column from Sql server manipulate
In our inventory database (SQL Server 2008 std edition) we have a table (called
I am using SQL Server and I have to group by a few columns
I have an existing MVC3 application and database that is on a SQL Server
I have some columns in a few tables in my database that are all
I have a few huge tables on a production SQL 2005 DB that need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.