Calculate the difference between two dates

Last post 11-07-2008 10:37 PM by destroyer. 13 replies.
Page 1 of 1 (14 items)
Active Topics My Discussions Unanswered Sort Posts: Previous Next
  • 10-03-2008 1:36 AM

    • Bruno
    • Not Ranked
    • Joined on 10-03-2008
    • Posts 2

    Calculate the difference between two dates

    Hello,

     I'm trying to write a query which will calculate the total between a start date in a table and today's date.

     Could someone advise?

     

    Thanks,Bruno

  • 10-03-2008 3:07 AM In reply to

    Re: Calculate the difference between two dates

    Hi,

    Welcome to the forums!!

    Refer datediff() in Books Online, this will calculate the difference between two dates.

    Hemantgiri S. Goswami | MS SQL Server MVP
    -------------------------
    "Humans don't have Caliber to PASS TIME , Time it self Pass or Fail Humans" - by Hemantgiri S. Goswami

    http://hemantgirisgoswami.blogspot.com
    http://forums.surat-user-group.org/

    View Hemantgiri S. Goswami's profile on LinkedIn

    Disclaimer: This post is provided as is, for the sake of knowledge sharing only.
  • 10-03-2008 3:41 AM In reply to

    Re: Calculate the difference between two dates

    What "Total" do you want to calculate?

    Can you give some more information? I guess you're after a something like

    SELECT SUM()
    FROM table
    WHERE date BETWEEN start AND end
    GROUP BY something

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Contributing Editor, Writer & Forum Moderator http://www.sql-server-performance.com
    Webmaster: http://www.insidesql.org
    View Frank Kalis's profile on LinkedIn

    XING
  • 10-03-2008 4:06 AM In reply to

    Re: Calculate the difference between two dates

    And perhaps he wants to know the difference between the totals for two different dates, with the sum calculated for one day, or cumulatively.

    It takes some work to get a straight question here.Stick out tongue

  • 10-03-2008 4:14 AM In reply to

    Re: Calculate the difference between two dates

    Yes. The question is ambigious. Post sample data with expected result

    Madhivanan

    Failing to plan is Planning to fail
  • 10-03-2008 6:44 AM In reply to

    • Bruno
    • Not Ranked
    • Joined on 10-03-2008
    • Posts 2

    Re: Calculate the difference between two dates

    Thank you for the feedback.

    I select the last row of the START_DT colomn and when I have that, I need to know the difference in days between that date and today's date in days?

    let's say that the latest row is '12/05/08' from START_DT, I need to know how many days there are between then and today.

     I hope its a bit more clear.

     

  • 10-03-2008 7:42 AM In reply to

    Re: Calculate the difference between two dates

    Okay - that's much simpler. You confused us by using "total" in your original question.Wink

    The "last row" is a classic problem here. In a table, any row can be the last row - you have to decide which way to order the data, in order to know what is the "last row".

    You seem to be looking for the most recent START_DT, rather than "the START_DT on the last row". This is the basic query:

    SELECT MAX(START_DT)
    FROM MyTable

    Now you elaborate on MAX(START_DT), putting it inside DATEDIFF() with the proper parameter for days. You can use the GETDATE() function to get the current date.

    That's it!

  • 10-03-2008 8:36 AM In reply to

    Re: Calculate the difference between two dates

    By the way, can there be rows in the table where START_DT is in the future?

    It doesn't make a difference for the calculation itself, but if you want to use the most recent (but not future) START_DT, please add a WHERE clause

    SELECT ............
    FROM MyTable
    WHERE START_DT <= GETDATE()

  • 10-03-2008 2:44 PM In reply to

    Re: Calculate the difference between two dates

    I would suggest you use the DATEADD function  If you are trying to calculate the total in days, months, years etc

    http://publib.boulder.ibm.com/infocenter/rbhelp/v6r3/index.jsp?topic=/com.ibm.redbrick.doc6.3/ssg/ssg51.htm 

     

     

  • 11-05-2008 11:57 PM In reply to

    Re: Calculate the difference between two dates

     hi...

    am trying to calculate the date difference between the System date and the table date.....

    the query which i wrote....

    select * from tbl_Invoice where DATEDIFF("GETDATE()","Date")<30

    [** where Date is the tbl_Invoice field name]

    But the problem is it not working ....it is showing error.....

     

    Plz.....help me.....Crying

    destroyer
  • 11-06-2008 12:47 AM In reply to

    Re: Calculate the difference between two dates

    This should work:

    select * from tbl_Invoice where DATEDIFF(GETDATE(),[Date])<30

    select * from tbl_Invoice where DATEDIFF(DAY, GETDATE(),[Date])<30

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Contributing Editor, Writer & Forum Moderator http://www.sql-server-performance.com
    Webmaster: http://www.insidesql.org
    View Frank Kalis's profile on LinkedIn

    XING
  • 11-06-2008 6:42 AM In reply to

    Re: Calculate the difference between two dates

    To make use of index, use

    select * from tbl_Invoice

    where

    [date]>=DATEDIFF(DAY, DATEDIFF(DAY,0,GETDATE()),0)

    [date]<DATEDIFF(DAY, DATEDIFF(DAY,0,GETDATE()),31)

    Madhivanan

    Failing to plan is Planning to fail
  • 11-06-2008 7:27 AM In reply to

    Re: Calculate the difference between two dates

    Good point about the index, but those DATEDIFF expressions will only return the number of days since day 0 and the number of days since day 31 ...

    WHERE dbo.tblInvoice.[date]
    BETWEEN DATEADD(DAY, DATEDIFF(DAY,0,GETDATE()), 0)
    AND DATEADD(DAY, DATEDIFF(DAY,0,GETDATE()),31)

  • 11-07-2008 10:37 PM In reply to

    Re: Calculate the difference between two dates

     Thanks.....its working.....

    Smile .................................Thanks...Once again........

    destroyer
Page 1 of 1 (14 items)
Active Topics   My Discussions    Unanswered Posts


© 2000 - 2007 vDerivatives Limited All Rights Reserved.