Selecting a row from excel from a particular ramge of date

Hi Everyone,

I have an excel with column name as Transaction date and various other column as well i have to extract all the rows from this excel whose date is between 15 and 31.
I have used this code HdfcSelcolumns.Select(“Transaction Date LIKE '%/”+Month+“/”+Year+"’ ").CopyToDatatable
Here HdfcSelColumns is a datatable and Transaction Date is a column in my excel and month and year is a string with month and year number.
I am getting an error saying Syntax error: Missing operand after ‘Date’ operator.

@aamir

try this query

(From p in dt.Select()
where convert.ToInt32((Convert.ToDateTime(p.Item(“Transaction Date”))).Day)>15 andconvert.ToInt32((Convert.ToDateTime(p.Item(“Transaction Date”))).Day)<31
select p).ToArray.CopyToDataTable

dt - your datatable variable

Regards,
Mahesh

Hi Mahesh,

getting error

Hi,

The above screenshot is wrong i have removed dt and kept my datatable. Still getting an error

And i forgot to mention i want date 15 and 31 also to be included in it

Hi Mahesh,

The above error is removed now however i am getting the below error now after running the query
Assign : String was not recognized as a valid DateTime.

Hi mahesh,

Any update as why that error?

Try This one @aamir
(From p in dt.Select() where convert.ToInt32((Convert.ToDateTime(p.Item("Transaction Date"))).Day)>=15 and convert.ToInt32((Convert.ToDateTime(p.Item("Transaction Date"))).Day)<=31 select p).ToArray.CopyToDataTable()

@aamir

Is your Excel sheet date is in this format, dd/MM/yyyy or dd-MM-yyyy

Else
if the transaction date is not in the above format try this,

(From p in dt.Select()

where convert.ToInt32(DateTime.ParseExact((p.Item(“Transaction Date”)).ToString,stringformat,System.Globalization.CultureInfo.CurrentCulture).Day)>15 and convert.ToInt32(DateTime.ParseExact((p.Item(“Transaction Date”)).ToString,stringformat,System.Globalization.CultureInfo.CurrentCulture).Day)<31

Select p).ToArray.CopyToDataTable()

Sry If it throws any syntax error because i have not tested this code…

Regards,
Mahesh

1 Like

Hi Mahesh,

My date format is dd-mm-yyyy

Hi Mahesh,

Got the following error

@aamir

You have to give “dd-MM-yyyy” in string format place

Regards,
Mahesh

Hi Mahesh,

Got the below error using the code below
(From p In HdfcSelcolumns.Select()
Where convert.ToInt32(DateTime.ParseExact((p.Item(“Transaction Date”)).ToString, “dd-MM-yyyy”,System.Globalization.CultureInfo.CurrentCulture).Day)>15 And convert.ToInt32(DateTime.ParseExact((p.Item(“Transaction Date”)).ToString, “dd-MM-yyyy”,System.Globalization.CultureInfo.CurrentCulture).Day)<31
Select p).ToArray.CopyToDataTable()
Assign : String was not recognized as a valid DateTime.

@aamir

is this “dd-MM-yyyy” the only format you have in your excel sheet

Regards,
Mahesh

I have this only format there is some time also associated with that which i am removing after ward let me remove that time at the initial stage.

Hi Mahesh,

i removed the time also now first only left with date still getting the same error should i attach my workflow with you.

@aamir

try with some rows to test by giving the date format as dd-MM-yyyy. then check whether it is giving the same error or not.

Your Datacolumn should have only this format “dd-MM-yyyy” I think it will work

Regards,
Mahesh

Shall i attach you my excel and code.

@aamir

That error is throwing because your datacolumn contains other format also…so just try once by creating one seperate excel sheet having a single date format…Even at that time also if it throws exception then you share your excel and xaml.

Regards,
Mahesh

Sure