Skip to main content
Inspiring
August 13, 2007
Answered

SQL dates

  • August 13, 2007
  • 1 reply
  • 648 views
This is my first time on an sql server. In my table, I have a start_date and end_date defined as datetime for the datatype. I just want to do a simple date calculation : <td>#datediff('d',qryGet_Report.start_date,qryGet_Report.end_date)#</td>
If the start_date and end_date are the same, the calculation comes out to zero. But if they are different, the calculation always appears to add one day, for example if start_date is 08/10/2007 and end_date is 08/12/2007, it should be 2 days, but it shows 3 days. I do not have this problem with access db, like I said, this is my first time on the sql server, so what is happening and what am I doing wrong ?
    This topic has been closed for replies.
    Correct answer trojnfn
    You could truncate the time portion either in your query or in CF. DateDiff() should return 2 if you compare dates only

    2007-08-01 00:00:00
    2007-08-03 00:00:00

    Got it. Used dateformat for both dates inside datediff and the calculation comes out correct now.

    Thanks for the help.

    1 reply

    Inspiring
    August 13, 2007
    If you add the 2 dates to your output, without formatting them, what are some typical values for the two dates and the datediff? Post your answer without formatting the dates.
    trojnfnAuthor
    Inspiring
    August 13, 2007
    The start date unformattted is 2007-08-01 10:02:16.0 and the end_date unformatted is 2007-08-03 08:29:30.0 and the date diff calculation comes out to 1 and it should be 2 ?

    Here is the date diff : #datediff('d',qryGet_Report.start_date,qryGet_Report.end_date)#
    Inspiring
    August 13, 2007
    DateDiff() returns the number of whole units difference. The difference between those two dates is approx 46hours. Since that's less than 2 days (ie 48hours), CF returns 1.