Simple DATEDIFF explanation

  • Hello.

    I just need a simple description of this query and its function.

    ROUND(DATEDIFF(d,'12/01/2009', DATEADD(d,-1,'12/01/2010'))/365.00,1) AS [Years]

  • Ricardumus (10/29/2010)


    Hello.

    I just need a simple description of this query and its function.

    ROUND(DATEDIFF(d,'12/01/2009', DATEADD(d,-1,'12/01/2010'))/365.00,1) AS [Years]

    It's a trick question. What textbook and chapter? We can probably help you figure out why they asked it.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • haha. No textbook. My boss wants an explanation of what this does.

  • Sorry for that then, the hard coded values are usually reserved for homework problems.

    This looks a LOT like a QotD type of trip up. Your boss quizzing your skills for possible promotion? I'm not convinced this is more then an unresearched question.

    I'll get you going in the right direction (crack open Books Online and bring up each of the functions also):

    DATEADD is what it sounds like. It takes a datetime value, and adjusts it by increments (first argument, 'd' = day) by how many increments you specify (second argument, -1).

    You then DATEDIFF. This is a comparison between two datetime values.

    You then divide by a numeric value. Note the .00 in the divisor. This is important.

    Look at the differences in the following results:

    PRINT 9/11

    PRINT 9/11.00

    You then round to a certain decimal position, which is another T-SQL function.

    Thus my comment about the trick question, you get a very... useless answer.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Thank you Craig. While useless, you have helped me. 🙂

    Now if i could only remember how to mark threads as solved. :-/

  • Ricardumus (10/29/2010)


    Thank you Craig. While useless, you have helped me. 🙂

    Now if i could only remember how to mark threads as solved. :-/

    Hopefully the answer was useless, and not the help. 😛

    You don't usually mark as solved here, though some folks will edit the subtitle as such.

    Sorry that I was vague on you. We try to avoid the homework solving crowd around here but I looked through your other posts and you're willing to learn, so I'm more then happy to help show you which windows to look through to find your way. 😀


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

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

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