How to find the number of days between two dates

后端 未结 9 578
独厮守ぢ
独厮守ぢ 2021-01-04 06:43

I have a basic query:

SELECT dtCreated
    , bActive
    , dtLastPaymentAttempt
    , dtLastUpdated
    , dtLastVisit
FROM Customers
WHERE (bActive = \'true\         


        
相关标签:
9条回答
  • 2021-01-04 07:30

    As @Forte L. mentioned you can do the following as well;

    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))
    
    0 讨论(0)
  • 2021-01-04 07:31
    DATEDIFF(d, 'Start Date', 'End Date')
    

    do it

    0 讨论(0)
  • 2021-01-04 07:32

    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
    

    so for your query:

    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))
    
    0 讨论(0)
提交回复
热议问题