gCoh
gCoh

Reputation: 3089

c# datatable new column Expression datetime

im trying to concatenate two different datetime fields in a datatable

the problem is, that i want to concatenate only the years, and not the whole datetime string.

for concatenating the entire string:

newColumn = new DataColumn("concatDate");

newColumn.Expression = "Convert(StartDate, 'System.String') 
                     + Convert(EndDate , 'System.String')";

any suggestions will be grate!!

cheers

Upvotes: 0

Views: 3712

Answers (1)

Steve
Steve

Reputation: 216358

Unfortunately there is no support for the YEAR method in the syntax recognized by the Expression property. However you could achieve your result using a very inelegant use of substring.

Something like this

newColumn.Expression = @"Substring(Convert(StartDate, 'System.String'), 7, 4) 
                       + Substring(Convert(EndDate, 'System.String'), 7, 4)"

The exact values to use for the Substring depend on your locale and on the rules applied when transforming your date in a string. So you should experiment with that 7 and 4 to adjust to your database settings

Upvotes: 1

Related Questions