[英]Not getting actual minutes in SQL DateDiff
I searched in different places and found below queries. 我在不同的地方搜索,发现下面的查询。 I am using the following queries to get the actual minutes difference in
SQL
. 我使用以下查询来获取
SQL
的实际分钟差异。 The dates I provide are the same day. 我提供的日期是同一天。 I need difference in minutes only but
SQL
is returning 35 instead of 5 minutes in the first query. 我只需要几分钟的差异,但
SQL
在第一个查询中返回35而不是5分钟。 And the second query return milliseconds. 第二个查询返回毫秒。
SELECT DATEDIFF(MINUTE,GETDATE(), CONVERT(datetime,'2016-08-11 16:04:24'))%3600/60 AS MinuteDiff
SELECT datediff(minute,GETDATE(), CONVERT(datetime,'2016-08-11 16:04:24')) as MinutesDiff
What is missing. 什么东西少了。 Please help.
请帮忙。
I need to put a condition that if time is less than 20 minutes then 我需要提出一个条件,如果时间少于20分钟那么
do this 做这个
else 其他
do this 做这个
Updated: 更新:
The issue occurs when i use GetDate(). 当我使用GetDate()时会出现此问题。 When I use a fix date it works fine
当我使用修复日期时,它工作正常
Hey sorry for the initial poor explanation. 嘿,对不起最初的糟糕解释。
I use something like the following frequently this will return a INT and decide if it's then you can do the logic on it, equal to, not equal less than greater than etc. 我经常使用类似下面的内容,这将返回一个INT并决定是否可以对其进行逻辑,等于,不等于大于等。
If it is true it will return a 1 or it is false a 0. You can get it to return columns or set it to a string. 如果为true,则返回1或者为false为0.您可以返回列或将其设置为字符串。
Hope it helps 希望能帮助到你
select
Case
When DateDiff(minute,[column],Getdate()) > 20 then 1 else 0
end as [alias]
You need to place the GETDATE()
after your datetime value, other wise in your case you will get the minutes in negative value. 您需要在日期时间值之后放置
GETDATE()
,否则在您的情况下,您将得到负值的分钟。
SELECT DATEDIFF(MINUTE,CONVERT(datetime,'2016-08-11 16:04:24'), GETDATE()) AS MinuteDiff
The current GETDATE()
is 2016-08-11 17:05:39.053
, so it returns 61
. 当前的
GETDATE()
是2016-08-11 17:05:39.053
,所以它返回61
。
Then based on the value, using IF ... ELSE ...
you can do your expected operation: 然后根据值,使用
IF ... ELSE ...
您可以执行预期的操作:
IF DATEDIFF(MINUTE,CONVERT(datetime,'2016-08-11 16:04:24'), GETDATE()) < 20
PRINT 'With in 20 mins'
ELSE
PRINT 'More than 20 mins'
Here is a working example of what your after...although you probably need to switch out the date components as appropriate for your usage. 以下是您的后续工作示例...尽管您可能需要根据您的使用情况切换出日期组件。
select
case
when
(SELECT datediff(minute,GETDATE(), CONVERT(datetime,'2016-08-11 06:00:00'))) < 20 then
(select 'do this')
else
(select 'do something else')
end as answer
If you want minute span between two datetime, then your second one is enough. 如果你想要两个日期时间之间的分钟跨度,那么你的第二个就足够了。
SELECT datediff(n, CONVERT(datetime,'2016-08-11 16:04:24'),GETDATE()) as MinutesDiff
you can use CASE
for your further 您可以进一步使用
CASE
select
case when
datediff(n, CONVERT(datetime,'2016-08-11 16:04:24'),GETDATE()) < 20 then
`your code`
else
`your else code`
end minte
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.