Pravin Kumar
Pravin Kumar

Reputation: 693

Filtering a Datatable using Select()

I have a DataTable (dt) as

 StartDate    EndDate    Price
  1              3          10
  4              6          12
  7              10          16
  11             15          20

I want to filter the Values from dates between fromdate= 2 and todate=8.

so i am expecting the result to be

 StartDate         EndDate     Price
      1              3          10
      4              6          12
      7              10         16

I have used the vb Datatable Select as->

dt.Select("StartDate <= #" & fromdate& "# And EndDate >= #" & todate& "#")

but i am not getting the result. Can u please suggest me..

Upvotes: 2

Views: 1782

Answers (3)

Szymon
Szymon

Reputation: 43023

Your query should be like that:

dt.Select("StartDate <= #" & todate & "# And EndDate >= #" & fromdate & "#")

to find records that overlap with your range.

Upvotes: 2

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

it should be

dt.Select("StartDate >= " & fromdate & " And EndDate <= " & todate)

Two problems

  1. Your operator were all wrong. You were taking startdate less than 2 and greater than 8 whereas it should be startdate greater than 2 and less than 8

  2. Morever no need for # assuming that columns are of integer type.

Upvotes: 1

Chris Allen
Chris Allen

Reputation: 605

Use the DateDiff function, my friend

Upvotes: -1

Related Questions