tags:

views:

127

answers:

4

I am trying to fetch records based on two dates from sql server... Select * from table where CreatedDate between @StartDate and @EndDate and i pass 5/12/2010 and 5/12/2010 (ie) fetching records for today... I have 17 records dated 5/12/2010 but none seems to get selected....

EDIT: I use this but when i debug my value it shows 5/12/2010 12:00:00AM

    DateTime baseDate = DateTime.Today;
    var today = baseDate;
    GetBookingReportByDate(today,today);

I am using these in c# and a resusable stored procedure which takes startdate and lastdate as parameters,

DateTime baseDate = DateTime.Today;

var today = baseDate;
var yesterday = baseDate.AddDays(-1);
var thisWeekStart = baseDate.AddDays(-(int)baseDate.DayOfWeek);
var thisWeekEnd = thisWeekStart.AddDays(7).AddSeconds(-1);
var lastWeekStart = thisWeekStart.AddDays(-7);
var lastWeekEnd = thisWeekStart.AddSeconds(-1);
var thisMonthStart = baseDate.AddDays(1 - baseDate.Day);
var thisMonthEnd = thisMonthStart.AddMonths(1).AddSeconds(-1);
var lastMonthStart = thisMonthStart.AddMonths(-1);
var lastMonthEnd = thisMonthStart.AddSeconds(-1);

I use these values and fetch records only based on startdate and lastdate... Exactly like stackoverflow Today,Yesterday,this week, last week,this month,Last month....

+4  A: 

You didn't include the time portion...so both are getting parsed to the same value.

You need:

SELECT *
FROM Table
WHERE CreatedDate >= '5/12/2010 00:00:00'
    AND CreatedDate <= '5/12/200 23:59:59'

Or:

SELECT *
FROM Table
WHERE CreatedDate >= @StartDate
    AND CreatedDate <= DATEADD(day, 1, @StartDate)

UPDATE

After seeing your update, changing the query like my second example would still work. You could also make the change in your C# code:

GetBookingReportByDate(today, today.AddDays(1));
Justin Niessner
@justin look at my edit i passing it from c#
chandru_cp
@chandru_cp - I updated my answer to clarify.
Justin Niessner
@justin for `Yesterday,this week, last week,this month,Last month` should i have to change any thing
chandru_cp
It all depends on the code you're using to call. I would suggest changing the SQL to only need one parameter, then change your C# method to take a single argument. That way there should be less code to change/write going forward.
Justin Niessner
I agree with Justin. Changing the sql parameters would also make your code more robust. If you accidentally swap start and end parameters now you will not get the correct results. I would either change the sql to accept a start date and duration (or end date and duration) or only a duration and hard code the end date as DateTime.Now() (as long as you don't need to include future times in your report).
AdmSteck
+2  A: 

The problem is that SQL is comparing both the date and time. Thus you query translates to CreatedDate Between '2010-05-12 00:00:00.000' And '2010-05-12 00:00:00.000'. Instead, you should do something like:

CreatedDate >= @StartDate And CreatedDate < DateAdd(d,1,@EndDate).

Thomas
+1  A: 

Try this instead:

Select *
from table
where CreatedDate >= @StartDate
    and CreatedDate < @EndDate

and set @EndDate to "tomorrow"

Daniel Renshaw
+1  A: 

If you don't want to change the sql statement, you can change the C# like this:

GetBookingReportByDate(today,today.AddDays(1));
AdmSteck
@Adam for `Yesterday,this week, last week,this month,Last month` should i have to change any thing
chandru_cp