我有一个基本的查询:
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”。
可以在查询中完成吗?
最佳答案
您将使用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))
关于sql - 如何找到两个日期之间的天数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11418496/