Get Data between two date ranges

  • Hello All,

    I am new to SQL coding and I need a help.

    I need to select data between two dates and this has to be run as stored procedure. When I hard code the dates I get the desired result but when I tired to used it's giving more no of records and data is not correct.

    Works when -->>> select * from table where event log >= '10/01/2016' and event log < '10/31/2016'

    Wrong/More data -->> select * from table where event log >= @StartDate and event log < DATEADD(dd,1,@EndDate)

    New to SQL coding.

    Any help is appreciated.

    Thanks.

  • Sounds like homework. So I'm not going to give you a direct answer, because then you wouldn't learn anything. And this forum isn't an answer machine...

    If you're trying to return all dates between 10/1/2016 and 10/31/2016 (or before 11/1/2016), then why not do that? If you assign '10/1/2016' as the value of @StartDate, how do you add a month to that? Basically, your logic sounds like it's right, but the way you've tried to write your filter is wrong. So skip the SELECT statement entirely first and just use something like:

    PRINT @EndDate;

    Once you figure out how to return the value you want (by doing some date math), then you can easily substitute into your query in your stored procedure.

  • Cawie (11/17/2016)


    Hello All,

    I am new to SQL coding and I need a help.

    I need to select data between two dates and this has to be run as stored procedure. When I hard code the dates I get the desired result but when I tired to used it's giving more no of records and data is not correct.

    Works when -->>> select * from table where event log >= '10/01/2016' and event log < '10/31/2016'

    Wrong/More data -->> select * from table where event log >= @StartDate and event log < DATEADD(dd,1,@EndDate)

    New to SQL coding.

    Any help is appreciated.

    Thanks.

    What's the datatype of the "Event Log" column (which cannot exist in your code without brackets because of the space, BTW)?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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