views:

75

answers:

5

Hi

All my times are in UTC timezone now I need to somehow convert it to the users timezone(I have it stored in the db as well and uses the ids of the windows timezones).

How can I do this in SQL Server 2005?

Edit

So I tried to do that extended stored procedure but with Timezoneinfo I get this error

Deploy error SQL01268: .Net SqlClient Data Provider: Msg 6503, Level 16, State 12, Line 1 Assembly 'system.core, version=3.5.0.0, culture=neutral, publickeytoken=b77a5c561934e089.' was not found in the SQL catalog. An error occurred while the batch was being executed.

if I take that line out I can deploy it. Any ideas how to get around this?

A: 

SQL Server does not provide a simple way to convert a UTC datetime value to a local time value.

However, the page that is from includes:

A table (tbTimeZoneInfo) with data to provide the Time Zone information and two functions to convert a UTC datetime value to any Local Time Zone.

Graphain
Ya I found this too but I can't figure out how to use. First I don't think I need one of their tables since all my timezone id's are from windows system time. So I am not sure UTC_TOLocalData but I may since of the offset. Plus the timezoneids look different.Other than I am not sure how to actually run it. I installed one of the scripts and tried to call the fucnction that it made but it told me it was not a system function.
chobo2
A: 

Wrote this just for you:

DECLARE @UTCDate DateTime /* Replace with the UTC datetime stored in your table */
DECLARE @LocalDate DateTime
DECLARE @TimeZoneOffset int /* Replace with the offset stored in your table */
SET @TimeZoneOffset = -8 /* PST */
SET @UTCDate = GETUTCDATE()
SET @LocalDate = DATEADD(Hour, @TimeZoneOffset, @UTCDate)
SELECT @UTCDate
SELECT @LocalDate

Let me know if it doesn't work.

Aaronontheweb
Great solution, the only problem is that it doesn't account for daylight savings time which is where things get really interesting.
James
Way to rain on my parade :( - we should all be like Arizona and just ignore DST :p
Aaronontheweb
Ya and another problem is I don't store the offset just the timezoneid.
chobo2
Sorry to rain on your parade! I've been through this painful exercise one too many times, and i'd hate to see someone else get burned ;)
James
A: 

Since SQL Server doesn't provide out of the box support for this, you might consider writing a .Net dll stored procedure, that makes use of the .Net TimeZoneInfo object , this object takes all rules including DST into consideration. This class allows you to convert time from one zone to another too. I hope this helps.

DateTime hwTime = new DateTime(2007, 02, 01, 08, 00, 00);
try
{
   TimeZoneInfo hwZone = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time");
   Console.WriteLine("{0} {1} is {2} local time.", 
           hwTime, 
           hwZone.IsDaylightSavingTime(hwTime) ? hwZone.DaylightName : hwZone.StandardName, 
           TimeZoneInfo.ConvertTime(hwTime, hwZone, TimeZoneInfo.Local));
}
catch (TimeZoneNotFoundException)
{
   Console.WriteLine("The registry does not define the Hawaiian Standard Time zone.");
}                           
catch (InvalidTimeZoneException)
{
   Console.WriteLine("Registry data on the Hawaiian STandard Time zone has been corrupted.");
}

[Edit]

Tutorial Creating Simple .Net DLL stored procedure.

Another useful tutorial, has more detail on deployment.

James
Can you shed more light on how to do this? So I make some .dll or something that can run in a sql stored procedure?
chobo2
@chobo2 - I added a new link to the bottom of my post. You'll have to enable .Net on your SQL box first. The only issue that might be a catch is that the TimeZoneInfo object is .Net 3.5 only. I believe calling .Net 3.5 from within SQL server dll proc should not be an issue as long as it's installed.
James
Hmm It does not like when I try to use TimeZoneInfo. see my edit.
chobo2
Did you install .net 3.5 on the SQL Server?
James
Ya I did(I have windows 7 so it is installed by default). Anyways I figured that out but still does not work see my other posts(it seems that I can't do what you suggested :( unless you know away around it)http://stackoverflow.com/questions/3209077/is-it-bad-to-put-with-permission-set-unsafe-in-sql-2005http://stackoverflow.com/questions/3209193/how-to-make-this-clr-work-with-2005
chobo2
Bummer - try this: http://social.msdn.microsoft.com/Forums/en-US/sqlnetfx/thread/d0515862-eb87-4a13-bab4-0e343983823a. If that does not work than this is going to get a bit messy. I would suggest that you make a web service that works with timezoneInfo, and then call the web service from within the dll proc.
James
A: 

SQL Server 2008 would have the DATETIMEOFFSET data type (which includes the time zone) plus functions like SWITCHOFFSET to switch from one timezone offset to another.

But on the 2005 version, there's not much support for timezones.

Any chance you could upgrade any time soon??

marc_s
Well probably earliest would be Jan 2011 so it won't help me till then.
chobo2
A: 

I use this function:

CREATE FUNCTION fnConvertGMTToLocalTime
(
    @GMTValue   Datetime,
    @TimeZoneOffset int
)
RETURNS DateTime
AS
BEGIN
    DECLARE @LocalTime dateTime

    SELECT @LocalTime = DateAdd(Hour, @TimeZoneOffset, @GMTvalue)

    RETURN @LocalTime
END

It doesn't consider daylight savings time...but i don't need that anyway.

Ed B