Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getdate() function to get date for my timezone

I would love to insert a default value into a column with data type datetime2(7). However, because my website is hosted on a server in a different timezone, the getdate function doesn't work properly. I wonder if there is a solution to this. I have done some research and found two ways. First is to use GetUTCDate() function. However, I would need to do the conversion when I display the information. I am sure my web application is used for only my timezone. So I would like to avoid this. Second way, this is the closest I could get this done by using SwitchOffSet function:

CREATE TABLE [dbo].[Test_Date](
[test_id] [int] NOT NULL,
[test_date] [datetime2](7) NOT NULL
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Test_Date] ADD  CONSTRAINT [DF_Test_Date_test_date]  DEFAULT (switchoffset(CONVERT([datetimeoffset],getutcdate()),'+13:00')) FOR [test_date]
GO

However, my problem is the +13:00 cause in the next few months, it will be +12:00 cause of the day light saving time change. As a result, I would need to change it every time. Anybody has a solution to this?

Thanks.

like image 909
loveprogramming Avatar asked Nov 20 '13 02:11

loveprogramming


People also ask

What timezone does Getdate () use?

GETDATE returns the current date and time in the current session time zone (UTC by default). It returns the start date or time of the current statement, even when it is within a transaction block.

How do I get current time zone in SQL?

SQL Server users typically fetch the local timezone by using the GETDATE() function with insert/update statements or as a default constraint for a datetime column in a select statement.

How do I get UTC date in SQL?

The GETUTCDATE() function returns the current database system UTC date and time, in a 'YYYY-MM-DD hh:mm:ss.

How do I only get date from Getdate?

Use CAST SELECT CAST(getdate() AS date); Or you can cast it to varchar: SELECT CAST(getdate() AS varchar(10));


3 Answers

SELECT GETDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time'

You need the first 'AT TIME ZONE UTC' in order to tell the DB what the value currently is in, so it knows how to get to the second given time zone, 'Central Standard Time' in my example.

like image 174
Don Cheadle Avatar answered Oct 13 '22 09:10

Don Cheadle


You can use SYSDATETIMEOFFSET function

select SYSDATETIMEOFFSET()

MSDN description:

Returns a datetimeoffset(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The time zone offset is included.

More on MSDN.


Based on clarification in the comment below:

Because you want to store the local time of the client, SQL Server has no way of knowing what is your local time. The best option that would work best would be to send the current time from the client each time.

like image 16
Szymon Avatar answered Oct 13 '22 08:10

Szymon


Since Sql Server 2016 you can use AT TIME ZONE...

SELECT CONVERT(datetime2(0), '2015-03-29T01:01:00', 126)     
AT TIME ZONE 'Central European Standard Time';  

... as specified in the documentation

like image 10
Mario Levrero Avatar answered Oct 13 '22 09:10

Mario Levrero