数据表计算方法转换字符串列最新 [英] Datatable Compute Method Convert string column to date

查看:132
本文介绍了数据表计算方法转换字符串列最新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据表中有日期,格式为 DD / MM / YYYY HH列:毫米。我使用code以下是超过1 select语句常见的,所以我不能填充数据表之前指定列和数据类型填写数据表。填充后的数据操纵任何可以接受我了。

I have a column in datatable having dates with format dd/MM/yyyy HH:mm. I fill the datatable using the code below which is common for more than 1 select statements so i cannot specify column and their datatype before filling the datatable. Any manipulation after filling the data is acceptable to me.

data_adapt = New OracleDataAdapter(query, OraConn)
dt = New DataTable
data_adapt.Fill(dt)

有关使用分页,并跳过下面走我创建DataTable的副本

For paging i create a copy of the datatable using skip and take as below

dtLineupCopy = New DataTable
dtLineupCopy = dtLineup.AsEnumerable().Skip(startRows).Take(pageSize)).CopyToDataTable()

现在的问题是,当我使用的方法计算它没有把列值作为日期类型和返回从列一些随机日期值,而不是最小值。

Now the issue is when I use Compute method it doesn't treat the column values as date type and returns some random date value from the column instead of minimum value.

Arvdate = dtLineupCopy.Compute("Min(Arrivaldate)", "")

有没有办法对数据类型转换为列?

Is there a way to convert the datatype for the column?

也尝试添加日期时间类型的新列,但它会抛出错误 System.FormatException:字符串未被识别为有效的DateTime

Also tried adding a new column of datetime type but it throws error System.FormatException: String was not recognized as a valid DateTime

dtLineupCopy.Columns.Add("ArvDate", getType(DateTime), "CONVERT(Arrivaldate, 'System.DateTime')")

在dtLineupCopy的Arrivaldate列中的数据。

Data in Arrivaldate column of dtLineupCopy.

22/09/2012 01:02结果
 27/09/2012 17:01结果
 1/10/2012 1:02结果
 13/10/2012 07:26结果
 14/10/2012 19:47结果
 20/10/2012 00:00结果
 20/10/2012 00:00

22/09/2012 01:02
27/09/2012 17:01
1/10/2012 1:02
13/10/2012 07:26
14/10/2012 19:47
20/10/2012 00:00
20/10/2012 00:00

推荐答案

如果您在结果传递Arrivaldate查询被带回作为一个字符串不是日期则preferred办法是改变这种状况的查询?而不是选择Arrivaldate的选择:

If the query that you pass in results in Arrivaldate being brought back as a string not a date then the preferred option would be to change that query? Instead of selecting Arrivaldate, select:

to_date(Arrivaldate, 'DD/MM/YYYY HH24:Mi') as Arrivaldate

如果不知何故,这不是,那么你解析字符串之后的一个选项。这样做的 DataTable.Compute 前pression的范围内是指滚动自己的解析日期插入排序格式功能类似...

If somehow that's not an option then you parse the strings afterwards. Doing that within the confines of the DataTable.Compute expression means rolling your own parsing date into sortable format function something like ...

Arvdate = dtLineupCopy.Compute("Min(Substring(Arrivaldate,7,4) + Min(Substring(Arrivaldate,4,2) + Min(Substring(Arrivaldate,1,2) + ... etc ..... " )) )

这篇关于数据表计算方法转换字符串列最新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆