How to filter DataView/DataTable by time? I have a table with datetime column inside and I need to filter by the time that is inside that datetime value. In native SQL it's not so hard but in ADO.NET Expressions it makes a dificulity for me.
+1
A:
If you want the filter to ignore the date component of the DateTime altogether, then I don't think this is possible by using the filter expression.
However, if filtering by both date and time suits your needs, then you could do something like this :
DataTable dt = new DataTable("t1");
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("DT", typeof(DateTime));
dt.Rows.Add(new object[] {1, new DateTime(2009, 1, 1, 14, 56, 27, 123)});
DateTime startDT = new DateTime(2009, 1, 1, 14, 56, 27, 123);
DateTime endDT = new DateTime(2009, 1, 1, 14, 56, 28, 123);
string filter = string.Format(
"DT >= '{0:yyyy-MM-ddTHH:mm:ss.fffffff}' AND DT <= '{1:yyyy-MM-ddTHH:mm:ss.fffffff}'",
startDT, endDT);
DataView dv = new DataView(dt, filter, null, DataViewRowState.CurrentRows);
MessageBox.Show("Num Rows selected : " + dv.Count.ToString());
Note the use of the ISO date/time format : "yyyy-MM-ddTHH:mm:ss.fffffff" in the filter.
Moe Sisko
2010-03-31 05:33:40
But I have to use the date that is in the dataset (DT in your example) and I need its <date> part only and filter by its <time> part. Is it possible?
Azat
2010-03-31 06:07:37
I don't think this is possible via Expressions.
Moe Sisko
2010-03-31 06:11:39
Well.. Thank you anyway :)
Azat
2010-03-31 06:12:57