简体   繁体   中英

Local time for specific time zone in SQL Server

I am in the process of porting an existing application to run on Windows Azure. Our current solution relies heavily on the time zone Windows is set to on the SQL Server. We use GETDATE() in our stored procedures and views quite frequently. The problem is that SQL Azure running using the UTC time zone. I'm trying to replace the GETDATE() method call with a method call that will get the time based on a setting in the database of what the time zone should be. The problem I'm having is Daylight Saving Time. Has anyone ever written a method in SQL that can return the current Date/Time and properly adjusting for DST?

I'm guessing that I'm going to get a few answers that will say that I should be using UTC times across the board. That does not appear to be an option for reasons I'd rather not explain.

Any help/suggestions you can give me would be greatly appreciated. Thanks.

We have decided to update the columns in question to be DateTimeOffset instead of DateTime .

Thanks to all those who responded.

I dont know if this will help you. But this executes a registry read on the SERVER registry. In the local context you can determine the difference between the SERVERs timezone and GMT and perofrm the necessary calcs you need from there. A usefull little trick I picked up from one of the other forums posts (Credit lies there).

Use Master
Go



DECLARE @ZONEDIFF INT

EXECUTE dbo.xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\TimeZoneInformation','ActiveTimeBias', @ZONEDIFF OUT

SELECT getdate() AS MyTime, dateadd(minute, @ZONEDIFF, getdate() ) as SERVERZONE

Let me know if it helps or if I am not understanding you correctly.

Mac

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM