0

I have a data table that contains dates in a format of yyyymmdd. I would like to add a datetime column to the table that contains the date value of that column.

Now the problem is that the table contains a lot of records and looping through all the records takes some time. Are there a way to do a bulk update or a use the Expression as from this post?

Something like:

table.Columns.Add("MyDate", typeof(DateTime)).Expression = 
DateTime.ParseExact("'strDateField'", "yyyymmdd", new CultureInfo("en-US"));

(Statement above does not compile which is expected, but only posted as to provide an example of what I'm trying to achieve)

Community
  • 1
  • 1
Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77

1 Answers1

1

Expression use a limit set of SQL functions exposed by .net for operations on data table columns, refer https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.110).aspx

You can try an arrangement like below

dt.Columns.Add("MyDate", GetType(Date)).Expression = "SUBSTRING(strDateField,5,2)+'/'+SUBSTRING(strDateField,7,2)+'/'+SUBSTRING(strDateField,1,4)";
Anil
  • 3,722
  • 2
  • 24
  • 49