Convert an integer column of a DataTable to DateTime to String Without For Each

Hello everyone, I need your help with this issue.

I have a DataTable with 8 Columns and a large number of Rows. The fact is that each cell has stored dates like this: “MM/dd/yyyy HH:mm:ss” and I want it in this format: “yyyyMMdd”.

I have tried with For each but there are many rows to iterate and it is not convenient, so I know that there are LINQ queries that facilitate this change of format for each column. I would like to know what it would be and how it would be implemented. Best regards

1 Like

Welcome to the forum

Get an introduction / starter Help resources by:
How to Update Data Column Values of a Data Table | Community Blog

[HowTo] LINQ (VB.Net) Learning Catalogue - Help / Something Else - UiPath Community Forum

Kindly note: Technically thee are options to do this, but it is highly suggested to find the balance between black box and traceability

1 Like

Yes, I have been using this statement:

dt = dt.AsEnumerable.Select(Function(x) x.Field(Of DateTime)("Column1").ToString("yyyyMMdd"))

But at the end of the statement it doesn’t let me place the .CopyToDataTable and that’s where I get stuck.

your statement is returning an Enumerable(Of String) - ~ a collection of strings
this datatype is not having a copytodatable method

I create this statement with what your post says and in theory it doesn’t give me any syntax problem but it returns this error:

System.ArgumentException: The input array is longer than the number of columns in this table.

The statement is this:

dtNew = (From d In dt.AsEnumerable Let gp = d.Field(Of DateTime)(“Column1”).ToString(“yyyyMMdd”) Let ra = d.ItemArray.Append(gp).ToArray Select dtNew.Rows.Add(ra)).CopyToDataTable

can you please share some sample data with us, thanks

the statement is related to its own case, so it will not serve in a different case

Once you had provided the sample data we can help with the solution. It is important to know the structure and which all data columns are to handle

It’s simple, imagine a DataTable that has 8 Columns and many rows where each value is a DateTime type displayed like this:

image As DateTime each value

y yo lo necesito de esta manera:

20210912, 20201112

The only way I managed to modify an entire column as I wanted is with this statement:

dt = dt.AsEnumerable.Select(Function(x) x.Field(Of DateTime)(“Column1”).ToString(“yyyyMMdd”)).ToArray

But of course, I don’t do anything like Array, I need to place it inside the DataTable. So here is the problem, I need each column to have the format I need

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.