Solved

Localtime To UTC

Posted on 2009-07-03
8
590 Views
Last Modified: 2012-05-07
Hi

How do I convert a smalldatetime (the local time) into the UTC time in SQL 2005? I can only see a getUTCdate() function in the system functions.

Thanks
0
Comment
Question by:silchester
  • 4
  • 3
8 Comments
 
LVL 31

Expert Comment

by:RiteshShah
Comment Utility
can't you use that function?
0
 
LVL 9

Expert Comment

by:drequinox
Comment Utility
use this code,
-- this is not my code, got it from internet somewhere a while ago , all credit goes to original author but would be nice to receive points for it though !!! :) , IF it works !!!
      DECLARE
          @offset TINYINT,
          @dt SMALLDATETIME,
          @sdt SMALLDATETIME,
          @edt SMALLDATETIME,
          DECLARE @i TINYINT
       
      SELECT
          @offset = 5,
          @i = 1,
          @dt = '20050201 05:32'
       
      -- find first Sunday in April
       
      WHILE @i < 7
      BEGIN
          SET @sdt = RTRIM(YEAR(@dt))+'040'+RTRIM(@i)
          IF DATEPART(weekday,@sdt)=1  
          BEGIN
              SET @i = 7
          END
          SET @i = @i + 1
      END
       
      -- find last Sunday in October
       
      SET @i = 31
      WHILE @i > 24
      BEGIN
          SET @edt = RTRIM(YEAR(@dt))+'10'+RTRIM(@i)
          IF DATEPART(weekday,@edt)=1  
          BEGIN
              SET @i = 24
          END
          SET @i = @i - 1
      END
       
      -- subtract hour from offset if within DST
       
      IF (@dt>=@sdt AND @dt<@edt)
          SET @offset = @offset - 1
       
      SELECT @dt AS CurrentTime, DATEADD(hour, @offset, @dt) AS UTCTime

 
0
 
LVL 31

Expert Comment

by:RiteshShah
Comment Utility
can't you use something like, find difference between your time and UTC time and add those time difference to your own dateField?

DateAdd(minute,DATEDIFF(minute, GetUtcDate(), GetDate()), YourDatefield)
0
 

Author Comment

by:silchester
Comment Utility
Sorry guys, do not think I explained this well:

I do not want to convert the current time to UTC.

I want to convert an arbitrary smalldatetime into its equivelent UTC.

So not wanting to refer to the current time at all, that is why the getUTCDate() function is not applicable.

So, want the UTC of for example: '9/22/2009 1:25:00 PM'

Hope this helps
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 31

Expert Comment

by:RiteshShah
Comment Utility
eventhough this will work,


select DATEADD(MINUTE,datediff(MINUTE,GETDATE(),getUTCdate()),getdate()),getutcdate()
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
Comment Utility
look at this:


declare @dt datetime
set @dt='9/22/2009 1:25:00 PM'
--select @dt
select DATEADD(MINUTE,datediff(MINUTE,GETDATE(),getUTCdate()),@dt)
0
 

Author Comment

by:silchester
Comment Utility
Hi Ritesh

Ok, I have entered your code into SQL 2005 Server Express SQL Query.

I must be being thick. But the results column shows :

2009-09-22 13:25:00.000

I was expecting an integer representing the number of seconds since midnight. I am just looking to see if I have to alter the results format....

I tried:

declare @utc_result AS bigint

SET @utc_result = <select DATEADD........ >

No luck...
0
 

Author Closing Comment

by:silchester
Comment Utility
Hi Ritesh
I reckon my question title was misleading - i am not bothered about localtime. so will close this question and ask a new one.
many thanks
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now