17

我有一个基本查询:

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”。

这可以在查询中完成吗?

4

10 回答 10

40

你会使用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))
于 2012-07-10T17:20:13.847 回答
9

要查找两个日期之间的天数,请使用:

DATEDIFF ( d, startdate , enddate )
于 2012-07-10T17:20:09.167 回答
2

我会使用 DATE_DIFF 函数来提供这个值,如下所示:

SELECT dtCreated
    , bActive
    , dtLastPaymentAttempt
    , dtLastUpdated
    , dtLastVisit
    , DATEDIFF(d, dtLastUpdated, dtCreated) AS Difference
FROM Customers
WHERE (bActive = 'true') 
    AND (dtLastUpdated > CONVERT(DATETIME, '2012-01-0100:00:00', 102))

编辑:如果使用 MySQL 你省略了 'd' 留下你

DATEDIFF(dtLastUpdated, dtCreated) AS Difference
于 2012-07-10T17:21:11.007 回答
1

如果您使用的是 MySQL,则有 DATEDIFF 函数可以计算两个日期之间的天数:

SELECT dtCreated
    , bActive
    , dtLastPaymentAttempt
    , dtLastUpdated
    , dtLastVisit
    , DATEDIFF(dtLastUpdated, dtCreated) as Difference
FROM Customers
WHERE (bActive = 'true') 
    AND (dtLastUpdated > CONVERT(DATETIME, '2012-01-0100:00:00', 102))
于 2012-07-10T17:23:05.533 回答
1
DATEDIFF(d, 'Start Date', 'End Date')

于 2016-03-31T09:37:21.427 回答
0

DATEDIFF函数用于计算所需日期之间的天数

例如,如果您以字符串格式区分当前日期与给定日期

SELECT * , DATEDIFF(CURDATE(),STR_TO_DATE('01/11/2017', '%m/%d/%Y')) AS days FROM consignments WHERE code = '1610000154'

这里,STR_TO_DATE () :取一个字符串,返回一个格式掩码指定的日期;

对于您的示例:

SELECT dtCreated
    , bActive
    , dtLastPaymentAttempt
    , dtLastUpdated
    , dtLastVisit
    , DATEDIFF(dtLastUpdated, dtCreated) as Difference
FROM Customers
WHERE (bActive = 'true') 
    AND (dtLastUpdated > '2012-01-01 00:00:00')

在 mysql 服务器 5.7.17 上测试

于 2018-08-31T09:29:51.517 回答
0

正如@Forte L. 提到的,您也可以执行以下操作;

SELECT dtCreated
    , bActive
    , dtLastPaymentAttempt
    , dtLastUpdated
    , dtLastVisit

    , DATEDIFF(day, dtCreated, dtLastUpdated) Difference

FROM Customers
WHERE (bActive = 'true') 
    AND (dtLastUpdated > CONVERT(DATETIME, '2012-01-0100:00:00', 102))
于 2019-02-20T22:55:51.777 回答
0
DECLARE @Firstdate DATE='2016-04-01',
 @LastDate DATE=GETDATE(),/*get today date*/
 @resultDay int=null

SET @resultDay=(SELECT DATEDIFF(d, @Firstdate, @LastDate))
PRINT @resultDay
于 2019-05-28T09:47:50.903 回答
0

获取两天之间的天数

DECLARE @date1 DATE='2015-01-01',
 @date2 DATE='2019-01-01',
 @Total int=null

SET @Total=(SELECT DATEDIFF(DAY, @date1, @date2))
PRINT @Total
于 2019-05-28T09:53:08.760 回答
0
DATEDIFF('Start Date', 'End Date')

在 MySQL 中,这给了我天数的差异。

于 2022-02-22T06:59:53.670 回答