Convert historical dates from utc to local time

I have a sql table with data like this:

| theDate (datetime)  | theValue (int) |
----------------------------------------
| 2010-05-17 02:21:10 |              5 |
| 2009-03-12 04:11:35 |             23 |
| 2010-02-19 18:16:53 |             52 |
| 2008-07-07 22:54:11 |             30 |

Dates are stored in UTC in the datetime column, how can I convert them to local time (Norway)? Remember that UTC offset is not the same throughout the year due to winter / summer time.

The UPDATE . I am using Microsoft SQL Server 2005, and I need to do this from SQL, because later the data will be shown in the report.

+5
source share
4 answers

This statement will work against the table you specify. Using logic, you can apply it to any date fields.

    SELECT CASE
         WHEN [theDate] BETWEEN 
         Convert(DATETIME, Convert(VARCHAR(4), Year([theDate])) + '-03-' + Convert(VARCHAR(2), (31 - (5 * Year([theDate])/4 + 4) % 7)) + ' 02:00:00', 20)
         AND
         Convert(DATETIME, Convert(VARCHAR(4), Year([theDate])) + '-10-' + Convert(VARCHAR(2), (31 - (5 * Year([theDate])/4 + 1) % 7)) + ' 03:00:00', 20)
         THEN Dateadd(hh, 2, [theDate])
         ELSE Dateadd(hh, 1, [theDate])
       END AS [theDate],
       [theValue]
FROM   [YOURTABLE] 
+3
source

CLR, SqlDateTime UTC CLR, - :

[Microsoft.SqlServer.Server.SqlFunction]
public static SqlDateTime ToLocalTime(SqlDateTime dt)
{
    try
    {
         return TimeZone.CurrentTimeZone.ToLocalTime(dt.Value);
    }
    catch
    {
        return SqlDateTime.Null;
    }
}
+2

PHP, ...

$dateFormat = 'r';
$tz         = 'Australia/Perth';
function convertDate($epochDate) {
  global $dateFormat, $tz;
  $tzOld = getenv("TZ");
  putenv("TZ=$tz");
  $ret = date($dateFormat, ($epochDate*1));
  putenv("TZ" . ($tzOld ? "=$tzOld" : ""));
  return $ret;
}
0
source

here is my function to return local winter time from local time

based on the time change on the last Sunday of March at 2:00 and on the last Sunday of October at 3:00

use your_DB
go
CREATE function [dbo].[ConvertToLocalWinterTime](@instime datetime)
returns datetime
as
begin
declare @localtm datetime = @instime
declare @localwintertm datetime
declare @lastmarchsunday datetime
declare @lastoctobersunday datetime
declare @tmpmd datetime = '03.31.' + convert(char(4),year(@localtm))
declare @tmpod datetime = '10.31.' + convert(char(4),year(@localtm))

    --- last sunday in march
    declare  @DOFWEEKM int = datepart (weekday,@tmpmd-1) 
    if @DOFWEEKM <7
    begin
    set @lastmarchsunday = @tmpmd - @DOFWEEKM 
    end
    else 
    begin
    set @lastmarchsunday = @tmpmd 
    end
    ----last sunday in October
    declare  @DOFWEEKO int = datepart (weekday,@tmpod-1) 
    if @DOFWEEKO <7
    begin
    set @lastoctobersunday = @tmpod - @DOFWEEKO
    end
    else 
    begin
    set @lastoctobersunday = @tmpod
    end
    -------------------------------
    if (@localtm > dateadd(hh,2,@lastmarchsunday)) and (@localtm < dateadd(hh,3,@lastoctobersunday))
    begin
    set @localwintertm =  dateadd(hh,-1, @localtm)
    end
    else 
    begin
    set @localwintertm = @localtm
    end

return @localwintertm
end
0
source

All Articles