Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store timezone in SQL Server 2005

I am building a web application where users can enter events, including

  • event title
  • start date/time
  • description

The users would like to enter the start date/time including a timezone that corresponds with the location of the event. The events are worldwide so the timezone can change from event to event.

In the SQL Server backend database, I am using datetime for the start date/time. What column type should I use to store the timezone? int? float? decimal?

like image 692
frankadelic Avatar asked Dec 01 '09 15:12

frankadelic


People also ask

How do I set timezone in SQL query?

USE time_converter; Step3: Creating table times with 2 columns using the following SQL query as follows. Let us create a table with index and DATETIME as a datatype. CREATE TABLE times (Sno INT, date_time DATETIME);

How do I display timezone in SQL?

For SQL Managed Instance, return value is based on the time zone of the instance itself assigned during instance creation, not the time zone of the underlying operating system. For SQL Database, the time zone is always set to UTC and CURRENT_TIMEZONE returns the name of the UTC time zone.

How do I store UTC datetime in SQL Server?

2 Answers. SELECT GETDATE() AS CurrentTime, GETUTCDATE() AS UTCTime. SELECT DATEADD(second, DATEDIFF(second, GETDATE(), GETUTCDATE()), YOUR_DATE);


2 Answers

Timezones are tricky, evil things. They're normally stored as a UTC offset, but even that has issues with regards to things like when daylight savings times change over (if at all).

If you're using Sql Server 2008, you can use a datetimeoffset type, which includes the utc offset with the value. Otherwise you'll need two columns.

like image 158
Joel Coehoorn Avatar answered Sep 19 '22 21:09

Joel Coehoorn


Since you are using SQL Server 2005, I would recommend storing the time zone as a string in the database, specifically a 32-character string since that is the limit on length for time zone identifiers in the Windows registry.

The values saved should be the values from the TimeZoneInfo ID property (e.g. "Eastern Standard Time") so that you can do calculations in the .NET Framework more easily.

As Joel said, time zones are evil and tricky. Good luck...

like image 28
mkedobbs Avatar answered Sep 19 '22 21:09

mkedobbs