How to find the number of days between two dates

后端 未结 9 577
独厮守ぢ
独厮守ぢ 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:14

    If you are using MySQL there is the DATEDIFF function which calculate the days between two dates:

    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))
    
    0 讨论(0)
  • 2021-01-04 07:14

    The DATEDIFF function is use to calculate the number of days between the required date

    Example if you are diff current date from given date in string format

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

    Here, STR_TO_DATE () : Take a string and returns a date specified by a format mask;

    For your example :

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

    Tested on mysql server 5.7.17

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

    To find the number of days between two dates, you use:

    DATEDIFF ( d, startdate , enddate )
    
    0 讨论(0)
  • 2021-01-04 07:22

    Get No of Days between two days

    DECLARE @date1 DATE='2015-01-01',
     @date2 DATE='2019-01-01',
     @Total int=null
    
    SET @Total=(SELECT DATEDIFF(DAY, @date1, @date2))
    PRINT @Total
    
    0 讨论(0)
  • 2021-01-04 07:26

    I would use the DATE_DIFF function to provide this value as below:

    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))
    

    EDIT: IF using MySQL you omit the 'd' leaving you with

    DATEDIFF(dtLastUpdated, dtCreated) AS Difference
    
    0 讨论(0)
  • 2021-01-04 07:27
    DECLARE @Firstdate DATE='2016-04-01',
     @LastDate DATE=GETDATE(),/*get today date*/
     @resultDay int=null
    
    SET @resultDay=(SELECT DATEDIFF(d, @Firstdate, @LastDate))
    PRINT @resultDay
    
    0 讨论(0)
提交回复
热议问题