How to convert between time zones in SQL Server 2008?

冷暖自知 提交于 2019-12-08 04:24:10

问题


I am using SQL Server 2008 R2 and I wanted to know how we can convert CST time to IST time. I already have CST time in my dtDate column and want to convert it to IST format. Can somebody please help me to get it.


回答1:


I will assume you mean "Cental Standard Time" and "India Standard Time" since "CST" and "IST" can mean a few different things. If this is wrong, just substitute the correct offsets for your timezones into something like this:

select cast(switchoffset(todatetimeoffset(dtDate, '-06:00'), '+05:30') as datetime)

Here are the steps:

  • Convert our datetime to a datetimeoffset, which is a time zone aware datatype.
  • Switch the offset to a new time zone.
  • Cast to a datetime (optional, but that seems to be what you want).



回答2:


I don't think this is a perfect answer. I don't know about the timezones you mention, but it doesn't help me.

I am based in the UK, within a US company and I produce reports in ET time zone. The system I extract data from holds all its dates in UTC. The server I run the reports on is hosted in NY and the times are ET.

I am reporting historical data and need to convert dates on tables into ET dates. I have been subtracting the difference between GETDATE() and GETUTCDATE() but I realise that I am getting that difference between those dates now, today. The dates I am converting are historical and, to cut a long story short, there are 4 hours difference between ET and UTC for 8 months of the year and 5 hours for the other 4 months.

If you are converting dates for zones with daylight savings you need a more sophisticated solution



来源:https://stackoverflow.com/questions/14478332/how-to-convert-between-time-zones-in-sql-server-2008

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