In SSMS, display DateTimeOffset value in local timezone

纵饮孤独 提交于 2019-12-11 06:34:22

问题


Isn't there a way to configure Sql Server Management Studio to, in query results, display the value of a DateTimeOffset column in the client's local timezone, instead of UTC?

Example: I'm in timezone +02:00. A value is stored as 2016-07-27 22:00:00.0000000 +00:00. Currently, this value is displayed as such in query results. I want it to be displayed (formatted) as 2016-07-28 00:00:00.0000000 +02:00 when executing this query on SSMS on my machine.

Currently I'm manually using something like SWITCHOFFSET(CONVERT(datetimeoffset, <DateColumn>), DATENAME(TzOffset, SYSDATETIMEOFFSET())) which is obviously very cumbersome.

If I'm not mistaken, in Oracle the NLS parameters on session level could be used for this. Is there something similar in Sql Server?


回答1:


Neither SQL Server nor SSMS have any ability to work with the concept of a "session time zone" (like Oracle, MySql, etc. do).

Also, it wouldn't make sense to automatically convert the offset anyway, as the offset is actually part of the stored datetimeoffset value.

If you're using SQL Server 2016, or Azure SQL Database, you can use the new AT TIME ZONE statement, as in:

SELECT yourdto AT TIME ZONE @thetimezoneid

Otherwise, consider using my SQL Server Time Zone Support project.

Neither of which can give you the system's time zone ID. You must know which time zone you want to work with.



来源:https://stackoverflow.com/questions/38655186/in-ssms-display-datetimeoffset-value-in-local-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!