November 17, 2016 at 8:09 pm
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.
November 17, 2016 at 9:42 pm
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.
November 17, 2016 at 10:22 pm
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
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply