Solved

Localtime To UTC

Posted on 2009-07-03
8
600 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
ID: 24771173
can't you use that function?
0
 
LVL 9

Expert Comment

by:drequinox
ID: 24771196
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
ID: 24771211
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:silchester
ID: 24771258
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
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24771267
eventhough this will work,


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

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24771271
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
ID: 24771380
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
ID: 31599508
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

806 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