简体   繁体   English

表达式“ System.Data.UnaryNode”中的类型不匹配

[英]Type mismatch in expression 'System.Data.UnaryNode'

I have a data table with a column (strDate) that contains values like '20150519'. 我有一个数据表,其中的一列(strDate)包含类似“ 20150519”的值。 I want to create a new DateTime column in the data table (without using a loop) that contains the date representation of strDate. 我想在数据表中创建一个新的DateTime列(不使用循环),该列包含strDate的日期表示。

I tried this: 我尝试了这个:

table.Columns.Add("NewDate", typeof(DateTime)).Expression = 
            "CONVERT((SUBSTRING(strDate, 0, 5) + '/' + 
                      SUBSTRING(strDate, 5, 2) +  + '/' + 
                      SUBSTRING(strDate, 7, 2)), 'System.DateTime')";

But it gives an error that: Type mismatch in expression 'System.Data.UnaryNode' 但是它给出一个错误: Type mismatch in expression 'System.Data.UnaryNode'

An underlying question here is: what Substring function is being used here? 这里的基本问题是:这里使用了什么Substring函数? SQL or .Net? SQL或.Net? From the documentation I understand that it is not SQL. 从文档中我了解到它不是SQL。 But .Net's Substring is a unary operator (which is inline with the error message), but then I would think my statement should look like: 但是.Net的Substring是一元运算符(与错误消息内联),但是随后我认为我的语句应类似于:

table.Columns.Add("NewDate", typeof(DateTime)).Expression = 
           "CONVERT((strDate).Substring(0, 4) + '/' + 
                    (strDate).Substring(4, 2) + '/' + 
                    (strDate).Substring(6, 2), 'System.DateTime')";

which in turns produce the error: 依次产生错误:

"Cannot interpret token '.' at position 21."

This solution eventually worked for me. 该解决方案最终对我有用。 Note that Substring is not 0 based, thus I would assume it is the SQL version of Substring. 请注意,Substring不是基于0的,因此我假设它是Substring的SQL版本。

table.Columns.Add("ChangeDate", typeof(DateTime)).Expression = 
                  "CONVERT(SUBSTRING([APO_CHDATE], 1, 4) + '/' + 
                           SUBSTRING([APO_CHDATE], 5, 2) + '/' + 
                           SUBSTRING([APO_CHDATE], 7, 2), 'System.DateTime')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Data.OleDb.OleDbException:条件表达式中的数据类型不匹配 - System.Data.OleDb.OleDbException: Data type mismatch in criteria expression system.data.oledb.oledbexception 标准表达式中的数据类型不匹配 - system.data.oledb.oledbexception data type mismatch in criteria expression 条件表达式中的数据类型不匹配 - Data type mismatch in criteria expression “条件表达式中的数据类型不匹配” - “Data type mismatch in criteria expression” 条件表达式中的数据类型不匹配 - Data type mismatch in criteria expression 条件表达式中的数据类型不匹配 - Data type mismatch in criteria expression 类型'System.Data.OleDb.OleDbException'的未处理异常附加信息:条件表达式中的数据类型不匹配 - An unhandled exception of type 'System.Data.OleDb.OleDbException' Additional information: Data type mismatch in criteria expression 'System.Data.OleDb.OleDbException:条件表达式中的数据类型不匹配。' 错误 - 'System.Data.OleDb.OleDbException: Data type mismatch in criteria expression.' error System.Data.OleDb.OleDbException:'条件表达式中的数据类型不匹配。 在C#中 - System.Data.OleDb.OleDbException: 'Data type mismatch in criteria expression. in c# System.Data.OleDb.OleDbException:“标准表达式中的数据类型不匹配。” C# 错误 - System.Data.OleDb.OleDbException: 'Data type mismatch in criteria expression.' C# error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM