need a single query for selecting dates

  • Hi,

    I need a single query for the following.

    i have to select the max (date) where the date between getdate()-5 and getdate() if the max(date) is null the have to select the max(date) of all dates from a table.

    Can any one help me...?

     

    Thanks,

    Rao Aregaddan.

  • Can we get the full problem (sample data, required output and table definition)... There's a few ways to do this but it depends on what comes before and after that code...

  •  

    ex:-1

    date    

    ------

    08-29-2006

    08-25-2006

    07-14-2006

    06-13-2005

    For the above we have records between getdate()-5 and getdate()

    so the output must be max(between getdate()-5 and getdate()) i.e   08-29-2006

    ex:-2

    date:

    ------

    01-26-2006

    12-07-2005

    04-04-2004

    11-08-2007

    For the above there are no records between getdate()-5 and getdate()

    so the output must be max(all records) i.e 11-08-2007

     

    So i need a single query which works for the above two conditions....

     

    Rao Aregaddan.

  • Using info from your 2 separate posts, I'm thinking this might be what you want...

    SELECT isnull(

        max(case when DEAL_DATE BETWEEN dateadd(day, -5, GETDATE()) AND GETDATE()

                 then DEAL_DATE end),

        max(DEAL_DATE))

    FROM DATA_PURCHASE

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • Simply superb    🙂

    Thanks allot for ur help........This query is working for my criteria.....Thanks once again...

     

    Rao Aregaddan.

     

     

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

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