从当地时间获取美国的日期时间(Get datetime for USA from local time)

我可以通过GetDate()函数获取本地sql服务器的时间。 Sql服务器机器在印度。

现在我想在同一个sql服务器上获得美国当前的日期时间,我可以做什么。

I can able to get time of local sql server by GetDate() function. Sql server machine is in india.

Now I want to get the current datetime of USA on same sql server what can I do.

最满意答案

您需要使用SYSDATETIMEOFFSET调用(而不是GETDATE() )来获取当地时间和时区信息:

SELECT SYSDATETIMEOFFSET()

然后您可以使用SWITCHOFFSET函数根据UTC时间定义您感兴趣的新时区。 所以美国东海岸的UTC时间为-5小时

SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '-05:00') AS 'US East Coast'

而西海岸是UTC -8小时:

SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '-08:00') AS 'US West Coast'

You need to use the SYSDATETIMEOFFSET call (instead of just GETDATE()) to get your local time with the timezone information:

SELECT SYSDATETIMEOFFSET()

and then you can use the SWITCHOFFSET function to define a new timezone you're interested in - based on UTC time. So the East Coast of the USA would be UTC -5 hours

SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '-05:00') AS 'US East Coast'

while the West Coast is UTC -8 hours:

SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '-08:00') AS 'US West Coast'

更多推荐