开发者

Concat columns adding data in datatable

I am getting some data from database that has date break up in year month and year due to some reason i want to concat the column and add a new column.

What i am doing is adding the data

                    DataColumn newColumn;
                    newColumn = new DataColumn("CompositeDate");
                    newColumn.Expression = "Day + Month + Year" ;
                    scaleResponseData.Columns.Add(newColumn);

the data in the data table is some thing like this

year | Month | Day
2009   10       2
2010   11       3

What my current code is doing

year | Month | Day | composite_Date 
2009   10       2      开发者_开发问答2021
2010   11       3      2024

But the result should be some thing

year | Month | Day | composite_Date 
 2009   10     02      20091002
 2010   11     03      20101103

I have different combination but nothing is working


Try this:

newColumn.Expression = "Convert(Day , 'System.String') + Convert(Month , 'System.String') + Convert(Year, 'System.String')";


This is because your columns are numbers and adding three numbers will yield a new number.

Try to force the expression to make a string by including an empty text between the columns:

newColumn.Expression = "Day + '' + Month + '' + Year" ;


Yoo can also convert that to "real" DateTime type, like this.

newColumn.Expression = "Convert(Year + '/' + Month + '/' + Day, 'System.DateTime')";


If you have year, month and date as ints in database they are added numerically, say: Year = 2009, month = 10 and day = 2 == 2009+10+2 = 2021, which is your current composite_date.

You should probably try year * 10000 + month * 100 + day to get the result you desire => newColumn.Expression = "Day + Month * 100 + Year * 10000" ;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜