如何在 SQL Server 2008 中的时区之间进行转换? [英] How to convert between time zones in SQL Server 2008?

查看:63
本文介绍了如何在 SQL Server 2008 中的时区之间进行转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 SQL Server 2008 R2,我想知道如何将 CST 时间转换为 IST 时间.我的 dtDate 列中已经有 CST 时间,并且想将其转换为 IST 格式.有人可以帮我弄一下吗.

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.

推荐答案

我假设您指的是中央标准时间"和印度标准时间",因为CST"和IST"可能意味着一些不同的东西.如果这是错误的,只需将时区的正确偏移量替换为如下所示:

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)

步骤如下:

  • 我们的日期时间转换为datetimeoffset,这是一种时区感知数据类型.
  • 将偏移量切换到新的时区.
  • 投射到日期时间(可选,但这似乎是您想要的).
  • 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).

这篇关于如何在 SQL Server 2008 中的时区之间进行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆