Median of Two dates

  • i have a requirement to find median of two date column in a table.

    Can anyone suggest how do i achieve it in sql query?

    Thanks for any help

  • keywestfl9 (4/21/2010)


    i have a requirement to find median of two date column in a table.

    Can anyone suggest how do i achieve it in sql query?

    Thanks for any help

    What would be the business reason for needing this? And what have you tried so far?

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • keywestfl9 (4/21/2010)


    i have a requirement to find median of two date column in a table.

    Can anyone suggest how do i achieve it in sql query?

    Thanks for any help

    How's this?

    DECLARE @Date1 datetime = '20080915',

    @Date2 datetime = GetDate()

    SELECT DateAdd(day, DateDiff(day, @Date1, @Date2)/2, @Date1)

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • For 2 exactly - the median is the average of the 2 numbers.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Matt Miller (#4) (4/21/2010)


    For 2 exactly - the median is the average of the 2 numbers.

    Well, keywestfl9 did say:

    i have a requirement to find median of two date column in a table.

    (emphasis mine)

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • WayneS (4/21/2010)


    Matt Miller (#4) (4/21/2010)


    For 2 exactly - the median is the average of the 2 numbers.

    Well, keywestfl9 did say:

    i have a requirement to find median of two date column in a table.

    (emphasis mine)

    Correct! I was just clarifiyng what the math (i.e. the one you showed) actually represented. Was also hoping to emphasize ths is the only place the calculation is this simply.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

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

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