Selecting dynamic date range

  • Hi,

    I have an sql query in Excel Existing connections through which i am able to pull the data. I am giving the required date range manually in sql query and pulling the data.

    Is there a possibility so that I can give the start date and the end date in excel. (Ex: Sheet1!A1 has the start date and Sheet1!B1 has the end date).

    Now the sql macro should take the input of start and end date from excel and pull the date.

    Please help me how to do that.

    Below is the sql query with me..

    select Naming, Date, Fields, Numbers

    from HJDU

    where Fields in (23, 25, 27)

    and

    Numbers in (1,2,3)

    and

    Date >=2013-09-01 and Date <=2013-09-30

    order by Date

    Here what i want dynamically is to pull the previous month data.

    Thanks in advance.

  • kishoremcp (10/2/2013)


    Hi,

    I have an sql query in Excel Existing connections through which i am able to pull the data. I am giving the required date range manually in sql query and pulling the data.

    Is there a possibility so that I can give the start date and the end date in excel. (Ex: Sheet1!A1 has the start date and Sheet1!B1 has the end date).

    Now the sql macro should take the input of start and end date from excel and pull the date.

    Please help me how to do that.

    Below is the sql query with me..

    select Naming, Date, Fields, Numbers

    from HJDU

    where Fields in (23, 25, 27)

    and

    Numbers in (1,2,3)

    and

    Date >=2013-09-01 and Date <=2013-09-30

    order by Date

    Here what i want dynamically is to pull the previous month data.

    Thanks in advance.

    Use the DATE functions. For the previous month this would be:

    SELECT Naming, Date, Fields, Numbers

    from HJDU

    where Fields in (23, 25, 27)

    and

    Numbers in (1,2,3)

    and

    WHERE DATE BETWEEN DATEADD(D,-DATEPART(d,GETDATE())+1,DATEADD(M,-1,CAST(GETDATE() as date)))

    AND DATEADD(D,-DATEPART(D,getdate()),CAST(GETDATE()as date))

    [font="Verdana"]Markus Bohse[/font]

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

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