我们如何在sql server中获得两个datetime的区别 [英] How we get difference of two datetime in sql server

查看:83
本文介绍了我们如何在sql server中获得两个datetime的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在getdate和数据库列日期得到20分钟的差异,但我的服务器在英国,我的国家时间和英国服务器时间不同。我的国家时间和服务器时间的差异是2小时



我尝试过:



 选择 @ currentdate = getDate(); 

会话中选择 LastAccess 其中 CAST(lastAccess As 日期)= Cast( @currentdate 作为 日期

选择 @ different_MINT = datediff(mint,currentdate,LastAccess)

如果 @ different_MINT > 20
开始
// 更新查询
结束

解决方案

尝试这:

  DECLARE   @ datetoChange  < span class =code-keyword> AS   DATETIME  
SELECT @ datetoChange = ' 2009-01-01 12:00:00.000 '

SELECT @ datetoChange
DATEDIFF (hh,GETUTCDATE(),GETDATE()) AS DIFFfromUTC,
DATEADD(hh,DATEDIFF(hh,GETUTCDATE(),GETDATE()),< span class =code-sdkkeyword> @ datetoChange ) AS modifiedDate




来源: SQL SERVER UTC发布日期至当地时间区域 [ ^ ]


How i get difference of 20 mints in getdate and database column date but my server is in uk and my country time and uk server time is different. difference of my country time and server time is 2 hour

What I have tried:

select @currentdate=getDate();

Select LastAccess from session where CAST(lastAccess As Date)=Cast(@currentdate As Date)

select @different_MINT=datediff(mint,currentdate,LastAccess )

if @different_MINT > 20
Begin
// Update Query
END

解决方案

Try this:

DECLARE @datetoChange AS DATETIME
SELECT @datetoChange = '2009-01-01 12:00:00.000'

SELECT @datetoChange, 
	DATEDIFF(hh,GETUTCDATE(), GETDATE()) AS DIFFfromUTC, 
	DATEADD(hh,DATEDIFF(hh,GETUTCDATE(), GETDATE()),@datetoChange) AS modifiedDate



Source: SQL SERVER UTC date to local time zone[^]


这篇关于我们如何在sql server中获得两个datetime的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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