Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 UTC v.s local time

Author  Topic 

nivea78
Starting Member

1 Post

Posted - 2007-03-07 : 22:50:51
I am a newbie to SQL server. I have datetime values stored as UTC in a table. What I want is that when I retrieve the records it should display these datetime values in local datetime. IS there a query or procedure someone can help me with?
Also is there some way of inserting the records in local time instead of UTC.

nivea

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-07 : 22:59:23
see
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=52026
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=28712

"is there some way of inserting the records in local time instead of UTC"
use getdate()
quote:

getdate()
Returns the current system date and time in the Microsoft® SQL Server™ standard internal format for datetime values.

getutcdate()
Returns the datetime value representing the current UTC time (Universal Time Coordinate or Greenwich Mean Time). The current UTC time is derived from the current local time and the time zone setting in the operating system of the computer on which SQL Server is running.




KH

Go to Top of Page

Paw Jershauge
Starting Member

2 Posts

Posted - 2007-04-04 : 13:10:47
Hey nivea78

Check my post on this link [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=28712[/url]

I had the same problem as you... im from denmark and i created this function:

CREATE FUNCTION dbo.ConvertToLocalTime(@InDateTime datetime)
-- Developed by Paw Jershauge
RETURNS DATETIME
AS
BEGIN
DECLARE @WorkingYear varchar(4) -- Opret en text variable som indeholder Året der skal bearbejdes
DECLARE @StartDay varchar(2) -- Opret en text variable som indeholder dagen i marts der svare til time skiftet
DECLARE @EndDay varchar(2) -- Opret en text variable som indeholder dagen i oktober der svare til time skiftet
DECLARE @SUMMERTIMESTART DATETIME -- Opret en DateTime variable som indeholder dato-tids værdien i marts for time skiftet
DECLARE @SUMMERTIMEEND DATETIME -- Opret en DateTime variable som indeholder dato-tids værdien i oktober for time skiftet
DECLARE @RETURNDATA DATETIME -- Opret en DateTime variable som indeholder den lokale dato-tids værdi
SET @StartDay = CONVERT(varchar(2),(31 - (5 * Year(@InDateTime)/4 + 4) % 7)) -- Algoritme som finder datoen i marts måned (virker indtil 2099)
SET @EndDay = CONVERT(varchar(2),(31 - (5 * Year(@InDateTime)/4 + 1) % 7)) -- Algoritme som finder datoen i oktober måned (virker indtil 2099)
SET @WorkingYear = CONVERT(varchar(4),Year(@InDateTime)) -- Sætter Års værdien
SET @SUMMERTIMESTART = CONVERT(DATETIME,@WorkingYear + '-03-' + @StartDay + ' 01:00:00',20) -- Sætter dato-tids værdien i marts for time skiftet
SET @SUMMERTIMEEND = CONVERT(DATETIME,@WorkingYear + '-10-' + @EndDay + ' 00:00:00',20) -- Sætter dato-tids værdien i oktober for time skiftet
IF DateDiff(hh,@SUMMERTIMESTART,@InDateTime) >= 0 AND DateDiff(hh,@SUMMERTIMEEND,@InDateTime) <= 0 -- Hvis datoen er i sommertid...
SET @RETURNDATA = DATEADD(hh,2,@InDateTime) -- lig 2 timer til UTC datoen
ELSE -- Hvis Datoen er uden for sommer tiden så...
SET @RETURNDATA = DATEADD(hh,1,@InDateTime) -- lig 1 timer til UTC datoen
RETURN @RETURNDATA -- Retunere Værdien
END
GO

--TEST YEAR 2005
Select dbo.ConvertToLocalTime('2005-03-27 02:00:00')
Select dbo.ConvertToLocalTime('2005-03-27 03:00:00')
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-04-04 : 15:29:29
It is best to have a table that has the DST start and end date for each year for the time zones you are interested in. The rules for when it changes can vary from one year to the next, according to the whim of the local government. As you may know, the rules for the US and Canada just changed this year.

Also, you have to be able to identify if the time zone you are converting from/to even observes daylight saving time, and what the offset is from UTC time during standard and daylight saving time.

I think it is better to put this information into a lookup table, than to try to do it as rules in a function, unless your data needs are very simple, like one time zone with rules that never change.





CODO ERGO SUM
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-04-08 : 11:11:02
very much agree with using a lookup table here.

not only do the DST rules change from year to year, but even the timezone you are in can change! Indiana is a case of this - several counties in Indiana changed timezones in 2006.


www.elsasoft.org
Go to Top of Page
   

- Advertisement -