Solved

Localtime To UTC

Posted on 2009-07-03
8
618 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

623 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