将日期参数添加到oracle查询

时间:2022-05-21 04:35:45

What I'm trying to do is to execute the query between two dates, and the dates would come from a parameter.

我想要做的是在两个日期之间执行查询,日期将来自参数。

In the below code, I'm putting one parameter and letting the second date as fixed just for example purposes.

在下面的代码中,我正在放置一个参数,并将第二个日期设置为固定仅用于示例目的。

The code / query is kind of long to paste it here, but here is a short version:

代码/查询有点长,可以在这里粘贴,但这里有一个简短的版本:

OracleParameter fromDateParameter = new OracleParameter();
fromDateParameter.OracleDbType = OracleDbType.Varchar2;
fromDateParameter.Value = "'3/06/2013 20:00:00'";

this.oracleDataAdapter4.SelectCommand = new OracleCommand("
   SELECT DISTINCT (LOG.RID) FROM LOG WHERE LOG.TIMESTAMP 
   BETWEEN TO_DATE(:fromDateParameter, 'MM/DD/YYYY hh24:mi:ss ') 
   AND TO_DATE('3/06/2013 23:59:00', 'MM/DD/YYYY hh24:mi:ss '))", 
   oracleConnection4);

oracleDataAdapter4.SelectCommand.Parameters.Add(fromDateParameter);           
this.oracleDataAdapter4.Fill(event11);

And I got this message:

我收到了这条消息:

ORA-01858: a non-numeric character was found where a numeric was expected

ORA-01858:找到了数字所在的非数字字符

I did the same query without the parameter and it works just fine.

我做了相同的查询没有参数,它工作得很好。

Thanks

1 个解决方案

#1


3  

I didn't find a way to do it using Varchar2 Datatype. So, I'm giving the answer using Date Datatype.

我没有找到使用Varchar2数据类型的方法。所以,我使用Date数据类型给出答案。

if (dateTo.Minute > 30)
   minToInt = 30;
else
   minToInt = 00;

dateFrom = DateTime.Now;
dateTo = DateTime.Now;     

DateTime dateFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, dateFrom.Hour, 00, 00);
DateTime dateTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, dateTo.Hour, minToInt, 00);

////////FROM DATE/////////
OracleParameter fromDateParameter = new OracleParameter();
fromDateParameter.OracleDbType = OracleDbType.Date;
fromDateParameter.Value = dateFrom;

////////TO DATE/////////
OracleParameter toDateParameter = new OracleParameter();
toDateParameter.OracleDbType = OracleDbType.Date;
toDateParameter.Value = dateTo;


this.oracleDataAdapter4.SelectCommand = new OracleCommand("
   SELECT DISTINCT (LOG.RID) FROM LOG WHERE LOG.TIMESTAMP 
   BETWEEN :fromDateParameter 
   AND :fromDateParameter)", oracleConnection4);

oracleDataAdapter4.SelectCommand.Parameters.Add(fromDateParameter);           
oracleDataAdapter4.SelectCommand.Parameters.Add(toDateParameter);  
this.oracleDataAdapter4.Fill(event11);

Don't get confuse with the if/else minute validation, it is just for my query purpose.

不要混淆if / else分钟验证,它只是为了我的查询目的。

#1


3  

I didn't find a way to do it using Varchar2 Datatype. So, I'm giving the answer using Date Datatype.

我没有找到使用Varchar2数据类型的方法。所以,我使用Date数据类型给出答案。

if (dateTo.Minute > 30)
   minToInt = 30;
else
   minToInt = 00;

dateFrom = DateTime.Now;
dateTo = DateTime.Now;     

DateTime dateFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, dateFrom.Hour, 00, 00);
DateTime dateTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, dateTo.Hour, minToInt, 00);

////////FROM DATE/////////
OracleParameter fromDateParameter = new OracleParameter();
fromDateParameter.OracleDbType = OracleDbType.Date;
fromDateParameter.Value = dateFrom;

////////TO DATE/////////
OracleParameter toDateParameter = new OracleParameter();
toDateParameter.OracleDbType = OracleDbType.Date;
toDateParameter.Value = dateTo;


this.oracleDataAdapter4.SelectCommand = new OracleCommand("
   SELECT DISTINCT (LOG.RID) FROM LOG WHERE LOG.TIMESTAMP 
   BETWEEN :fromDateParameter 
   AND :fromDateParameter)", oracleConnection4);

oracleDataAdapter4.SelectCommand.Parameters.Add(fromDateParameter);           
oracleDataAdapter4.SelectCommand.Parameters.Add(toDateParameter);  
this.oracleDataAdapter4.Fill(event11);

Don't get confuse with the if/else minute validation, it is just for my query purpose.

不要混淆if / else分钟验证,它只是为了我的查询目的。