views:

335

answers:

3

How do I filter for records on a DataTable which are on a certain date?

I've tried plain [datecol] = #11 March 2010# and CONVERT([datecol],'System.DateTime') = #11 March 2010#. With no luck.

MSDN: RowFilter Expression Syntax

Solution

[datecol] >= #11 March 2010 00:00# AND [datecol] <= #11 March 2010 23:59:59#
+1  A: 
select 
   ...
   ...
   ...

where [datecol] between '11 March 2010 00:00:00' and '11 March 2010 23:59:59'

Sorry, wrong head on. Just been writing a LOT of SQL!!

Try this

[datecol] >= #03/11/2010 00:00:00# AND [datecol] <= #03/11/2010 23:59:59# 

or

 [datecol] >= '03/11/2010 00:00:00' AND [datecol] <= '03/11/2010 23:59:59' 
CResults
RowFilter doesn't use SQL
Sam
Although I can convert it for the RowFilter
Sam
You're lucky I have two heads!
CResults
+1  A: 
select * from [X] WHERE DATEDIFF(dd,[datecol],'3/11/2010') = 0

There is a lot of power with the datediff function,

DATEADD(DAY,DATEDIFF(DAY,0,[datecol]),0) is another way you can strip the time portion from the column if you need that data for other processing. We use this if we're wanting to group items that occur during the day, or some other uses if we need to group items that occur during hours or after hours.

Digicoder
Thanks, would like to use datediff, but RowFilter doesn't query the database
Sam
The idea is to format the data you want upon input, so that you don't get too much data. If the input data is fixed and you have no control, then using RowFilter, you would need to generate your string. It would basically be using StringBuilder() and generating the string "[datecol] > #11 March 2010 00:00# AND [datecol] < #11 March 2010 23:59:59#"
Digicoder
+1  A: 

Hi,

You can use following LINQ code to filter DataTable row-set according to date:

var resultSet = from tbl in dt.AsEnumerable()
                where tbl.Field<DateTime>("ColName").ToString("dd/MMM/yyyy") == "07/Aug/2010"
                select tbl;

Further, to get the insight of basic LINQ queries on DataTables see the post Filtering DataTable using LINQ

best Rgds

Awais