I have tried quering the data in the database - or more specific in uCommerce_PurchaseOrder, where I need to get orders within a daterange, where the column CompletedDate is a datetime allowing null values.
var sqlQuery = "SELECT * FROM uCommerce_PurchaseOrder o";
var sql = new Sql(sqlQuery);
Sql filterSql = new Sql();
var pageIndex = 0;
var pageSize = 100;
var start = new DateTime(2018, 1, 1);
var end = DateTime.Now;
Sql filterSql = new Sql();
filterSql.Where("o.CompletedDate IS NOT NULL")
.Where("o.CompletedDate >= @start", new { start = start }, DatabaseContext.SqlSyntax)
.Where("o.CompletedDate <= @end", new { end = end }, DatabaseContext.SqlSyntax);
sql.Append(filterSql);
// Get page of results and total count
var pagedResult = _db.Page<OrderModel>(pageIndex + 1, pageSize, sql.SQL);
int totalRecords = Convert.ToInt32(pagedResult.TotalItems);
With the .Where() clauses for start and end date it doesn't return any results, but without these it does.
I have tried with a different format as well without luck:
filterSql.Append("WHERE o.CompletedDate IS NOT NULL");
filterSql.Append("AND o.CompletedDate >= @start AND o.CompletedDate <= @end",
new
{
start = start,
end = end
}
);
Just in case someone else queries this too, I used the standard BETWEEN clause in SQL:
.Where(@"EventsId IN (@eventsIds) AND ( (StartDAte BETWEEN @1 AND @2) OR (EndDate BETWEEN @1 AND @2) OR (StartDate <= @1 AND EndDate >= @2))",
new { eventsIds = eventsIds },
fromDate.Date,
toDate.Date)); // + 1 to make sure the we find a full day.
This finds all events that start or end within the date range and for events that span the selected date range.
Query data between two dates with PetaPoco
I have tried quering the data in the database - or more specific in uCommerce_PurchaseOrder, where I need to get orders within a daterange, where the column CompletedDate is a
datetime
allowing null values.With the
.Where()
clauses for start and end date it doesn't return any results, but without these it does.I have tried with a different format as well without luck:
Any idea what I am missing in the queries?
/Bjarne
I found another way to solve this, but I don't know why regular SQL expression didn't seem to work with the datetime parameters.
Just in case someone else queries this too, I used the standard BETWEEN clause in SQL:
This finds all events that start or end within the date range and for events that span the selected date range.
is working on a reply...