SQL Server Performance

Date Diff..

Discussion in 'T-SQL Performance Tuning for Developers' started by bugatha, May 2, 2007.

  1. bugatha New Member

    Is there any function which gives the date difference for no of dates as datediff() gives the difference between only two specific dates.

    Hema Sunder.
  2. FrankKalis Moderator

    Can you give an example? I'm not sure I understand you.

    Frank Kalis
    Microsoft SQL Server MVP
  3. bugatha New Member

    Let me explain in detail:

    DATEDIFF ( datepart , startdate , enddate ) gives difference between only two specific dates.

    I have multiple startdates and end dates and want the difference between those dates.

  4. FrankKalis Moderator

    Do you mean you have a table with one startdate and one enddate and now want to have the DATEDIFF for each row? Or do you have multiple startdate and enddate columns in your table?<br /><br />Sorry, but I define "detailed" a bit different than you. [<img src='/community/emoticons/emotion-1.gif' alt=':)' />]<br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a>
  5. bugatha New Member

    I want datediff for multiple records at a time.
  6. khtan New Member

    It is not very clear on what you want.

    Please post your table DDL, some sample data and the result that you want

  7. Adriaan New Member

    Why not use a correlated subquery like this:

    SELECT t1.mydate,
    DATEDIFF(day, (SELECT MAX(t2.mydate) FROM tbl t2 WHERE t2.mydate < t1.mydate), t1.mydate)
    FROM tbl t1
    ORDER BY t1.mydate
  8. Madhivanan Moderator

    quote:Originally posted by bugatha

    I want datediff for multiple records at a time.
    Dont Let everyone guess what you want
    As Tan said, post some sample data and the result you want


    Failing to plan is Planning to fail
  9. Adriaan New Member

    The query I posted gives him the DateDiff between the current row and the previous row, when sorted by the date column.

Share This Page