如何在SQL Server中将IST时间转换为EST时间 [英] How to convert IST time to EST time in SQL server

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

问题描述

我正在写入一个触发器,然后在此表的表一列中插入ReceivedDatetime捕获IST时间但现在想要在EST时间



需要转换ReceivedDatetime列应该是美国东部时间不是IST时间。

可以任何人建议我。



我尝试过:



如何在sql server中将IST时间转换为EST时间

i am traing to write one trigger before insert in table one column of this table ReceivedDatetime capturing IST time but now want in EST time

Need to convert ReceivedDatetime column should be in EST time not IST time.
can any one suggest me please.

What I have tried:

how to convert IST time to EST time in sql server

推荐答案

你不应该在EST或IST中存储时间......请使用UTC日期。

即使用 GETUTCDATE()而不是 GETDATE ()



所有时间调整都应该使用表示层中的Locale完成(我无法进一步评论如何执行此操作因为你还没有说你是如何呈现这些数据的)



部分问题是必须调整那些夏令时或夏令时使用它的时区 - EST就是其中之一(又名EDT)



例如,解决方案2中的博客链接指出EST是UTC-5,但如果EDT到位,这将更改为UTC-4。



参考文献:

EST - 东部标准时间(时区)缩写) [ ^ ]

EDT - 东部夏令时(时区缩写) [ ^ ]



所以请注意如果您以特定格式存储日期,则可能无法向用户显示正确的时间。



要将现有列转换为UTC时间,您可以使用GETUTCDATE和GETDATE的组合,用于确定当前时间(在SQL服务器上)和UTC时间之间的差异。



例如,我使用过生成的一些示例数据如下:

You should not being storing times in either EST or IST ... use UTC dates instead.
i.e. use GETUTCDATE() rather than GETDATE()

All time adjustments should be done using the Locale in your presentation layer (I can't comment further on how to do this as you haven't said how you are presenting this data)

Part of the problem is having to adjust for "Daylight Saving Time" or "Summer Time" in those timezones that use it - EST being one of them (aka EDT)

For example the blog link in Solution 2 states that EST is UTC-5 but this will change to UTC-4 if EDT is in place.

References:
EST - Eastern Standard Time (Time Zone Abbreviation)[^]
EDT - Eastern Daylight Time (Time Zone Abbreviation)[^]

So be aware that if you do store the dates in a specific format you still may not be displaying the correct time to the user.

To convert an existing column into UTC time you can use a combination of GETUTCDATE and GETDATE to determine the difference between the current time (on the SQL server) and UTC time.

For example, I used some sample data generated as follows:
WITH q AS
    (
    SELECT  GETDATE() AS datum
    UNION ALL
    SELECT  dateadd(hh, 5, datum)
    FROM    q
    WHERE dateadd(hh, 5, datum) < dateadd(dd, 15, GETDATE())
    )
SELECT  datum, cast(null as datetime) as datum2
into #temp
FROM    q

(基本上只是一个从现在开始的日期时间列表,每行增加5个小时)

要将列datum2设置为UTC时间,我可以使用以下内容:

(Basically just a list of datetimes starting from now and adding 5 hours per row)
To set column datum2 to UTC time I can use the following:

update #temp set datum2 = DATEADD(hh, (-1) * (DATEDIFF(hh,GETUTCDATE(),GETDATE())), datum)



如果你真的必须将数据存储在EST中,然后修改代码行以便随后进行转换(即先将时间转换为UTC然后转换为目标时区)。例如


If you really muststore the data in EST then just amend the line of code to subsequently do that conversion (i.e. take the times to UTC first then convert to your target timezone). E.g.

update #temp set datum2 = DATEADD(hh, - 5, DATEADD(hh, (-1) * (DATEDIFF(hh,GETUTCDATE(),GETDATE())), datum))


请使用此链接



SinghVikash Blog - SQLServer.GETUTCDATE()函数 - Vikash Kumar Singh [ ^ ]



Ashish
Please use this link

SinghVikash Blog - SQLServer.GETUTCDATE() function - Vikash Kumar Singh[^]

Ashish


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

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