如何找到两个日期之间的天数 [英] How to find the number of days between two dates
本文介绍了如何找到两个日期之间的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个基本查询:
SELECT dtCreated
, bActive
, dtLastPaymentAttempt
, dtLastUpdated
, dtLastVisit
FROM Customers
WHERE (bActive = 'true')
AND (dtLastUpdated > CONVERT(DATETIME, '2012-01-0100:00:00', 102))
我想在输出中添加另一列...让我们称其为差异"以找出dtcreated"和dtlastupdated"之间的天数 例如,如果记录 1 的 dtcreated 为 1/1/11 和 dtlastupdated 是 1/1/12,差异"列将是365".
I want to add another column to the output... lets call it "Difference" to find out the number of days between 'dtcreated' and 'dtlastupdated' So for example if record 1 has a dtcreated of 1/1/11 and dtlastupdated is 1/1/12 the "Difference" column would be "365".
这可以在查询中完成吗?
Can this be accomplished in a query?
推荐答案
您可以使用 DATEDIFF
:
You would use DATEDIFF
:
declare @start datetime
declare @end datetime
set @start = '2011-01-01'
set @end = '2012-01-01'
select DATEDIFF(d, @start, @end)
results = 365
对于您的查询:
SELECT dtCreated
, bActive
, dtLastPaymentAttempt
, dtLastUpdated
, dtLastVisit
, DATEDIFF(d, dtCreated, dtLastUpdated) as Difference
FROM Customers
WHERE (bActive = 'true')
AND (dtLastUpdated > CONVERT(DATETIME, '2012-01-0100:00:00', 102))
这篇关于如何找到两个日期之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文