How the datediff determinines the difference between two dates?

  • select datediff(wk, '2015-01-11', '2015-01-19') returns 1 (shouldn't it return 2?)

    select datediff(wk, '2015-01-10', '2015-01-19') returns 2

    Thanks

  • I believe the difference in those calculations will use SQL Server's numbered weeks; this will change based on your DATEFIRST settings, but, assuming a DATEFIRST of Monday:

    1/11/2015 is one full week before 1/19/2015; you can see the basis for this by running a SELECT DATEPART(WEEK,'1/11/2015'), which should return a week number of 3.

    On the other hand, 1/10/2015 is two full weeks before 1/19/2015; again, a SELECT DATEPART(WEEK,'1/10/2015') will show as much, with a week number of 2.

    Hence, the difference between today, which is in week number 4, and 1/10/2015's week, which is 2, is 2. For 1/11/2015, the difference is 4 and 3, which is one.

    - 😀

  • sonchoix (1/19/2015)


    select datediff(wk, '2015-01-11', '2015-01-19') returns 1 (shouldn't it return 2?)

    select datediff(wk, '2015-01-10', '2015-01-19') returns 2

    Thanks

    As per BoL: "DATEDIFF ... Returns the count (signed integer) of the specified datepart boundaries crossed between the specified startdate and enddate"

    Week boundary will depend on DATFIRST setting (default - 7 - Sunday for U.S. English)

    So, datediff will count a week for each crossing of week boundary starting from startday (if startday is a boundary day, it's not counted as there is no crossing of 23:59:59 to 00:00:00...)

    Your first pair has one crossing, therefore DATEDIFF returns 1,

    the second one has two corssings, hence 2 is returned.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Thank you guys

  • Be advised that the "wk" DATEPART can change depending on language settings and DATEFIRST settings. It's not considered to be a good thing to use.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (1/19/2015)


    Be advised that the "wk" DATEPART can change depending on language settings and DATEFIRST settings. It's not considered to be a good thing to use.

    +1

    I would calculate the difference in days and get the modulo of 7.

    (which might be a total different interpretation of difference in weeks)

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply