Solved

Localtime To UTC

Posted on 2009-07-03
8
608 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Help with Data Warehouse / Data Marts 4 42
Creating a View from a CTE 15 48
File attachment in the SQL Database from application 10 47
learning MS SSIS 13 20
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

739 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