使用DataView.RowFilter过滤记录时转换列的数据类型 [英] Convert datatype of column while filtering records using DataView.RowFilter

查看:74
本文介绍了使用DataView.RowFilter过滤记录时转换列的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,其中的列具有日期.日期存储为

I have a datatable with a column having Date. Dates are stored as

2018/11/14 @ 12:10 PM.

2018/11/14 @ 12:10 PM.

2018/11/15 @ 13:49下午.

2018/11/15 @ 13:49 PM.

2018/11/16 @ 15:37下午等等

2018/11/16 @ 15:37 PM. and so on

现在我必须仅根据日期过滤记录.

Now I have to filter records on the basis of date only.

我正在使用以下代码过滤记录,但未返回正确的数据.

I am filtering records using below code but it is not returning correct data.

dv.RowFilter = "SUBSTRING(DateColumn,1,10)>='2018/11/01' AND SUBSTRING(DateColumn,1,10)<='201811/30'";

我尝试像在Sql中一样强制转换子字符串值

I tried to cast substring value as we do in Sql

(cast(DateColumn as date))

但是会引发错误.

任何人都可以帮助我如何从该列中获取日期部分并将其数据类型更改为日期.

Can anyone help me how I can get date part from that column and change it's data type to date.

推荐答案

为了将给定列中的值转换为日期,您需要使用 CONVERT

In order to convert the value in a given column to a date you need to use the CONVERT function, rather than cast.

(cast(DateColumn as date))

将成为:

CONVERT(DateColumn, 'System.DateTime')

不幸的是,如果您的字符串格式可以很容易地转换为 DateTime (尽管这样做肯定不会损害它的机会),我会感到惊讶.像这样可能会给您带来一些成功:

Unfortunately I'd be surprised if the format of your string is one that will readily convert to a DateTime (though trying surely won't hurt on the off chance that it does!), something like this may give you some success though:

CONVERT(SUBSTRING(DateColumn, 1, 10), 'System.DateTime')

然后您可以对其应用过滤器并说:

To which you can then apply your filter and say:

dv.RowFilter = "CONVERT(SUBSTRING(DateColumn, 1, 10), 'System.DateTime') >= '2018/11/01'";

这篇关于使用DataView.RowFilter过滤记录时转换列的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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