[英]How to solve SQL error: ORA-01843: not a valid month
Any idea what is wrong with my query? 知道我的查询出了什么问题吗?
I am trying to update a row of my Database but the query I am using causes exception. 我正在尝试更新数据库的一行,但是我正在使用的查询导致异常。
UPDATE Table1
SET CHANGE_DELIVERY_REASON = 'Card Activation',
CHANGE_DELIVERY_NOTE = 'ACTIVATION SUCCESS [1 OK - 2 OK]',
RECONCILIATION_STATUS = 1 ,
RECONCILIATION_LAST_UPDATED = '23/4/2018 12:00:00 AM',
RECONCILIATION_COUNT = '6',
ACTIVATION_FAILURE = 'SUCCESS';
I am getting this error: 我收到此错误:
"SQL Error: ORA-01843: not a valid month"
“ SQL错误:ORA-01843:无效月份”
I am not figuring out what is wrong with my query. 我没有弄清楚我的查询出了什么问题。
updateQuery = " UPDATE " + clsUtility.GetMasterTable() + " SET ";
updateQuery += " CHANGE_DELIVERY_REASON = 'Card Activation', ";
updateQuery += " CHANGE_DELIVERY_NOTE = '" + notes + "', ";
updateQuery += " RECONCILIATION_STATUS = 1 , ";
updateQuery += " RECONCILIATION_LAST_UPDATED = '" + DateTime.Today + "', ";
updateQuery += " RECONCILIATION_COUNT = '" + reconciliation_count + "',";
updateQuery += " ACTIVATION_FAILURE = '" + resultPart + "', ";
Your problem is the format of the date you gave, this was handled in the other answers. 您的问题是您给定的日期格式,这在其他答案中得到了解决。 You will need this for other date fields, so definitely look at it.
您将在其他日期字段中使用此功能,因此请务必进行查看。
However, for this special case, you could simply stop using your local date and let the database do the work: 但是,对于这种特殊情况,您可以简单地停止使用本地日期,然后让数据库完成工作:
RECONCILIATION_LAST_UPDATED = trunc(sysdate)
And on a sidenote: please use parameter binding in your queries. 另外,请注意:请在查询中使用参数绑定。 You are wide open to SQL injection attacks.
您对SQL注入攻击持开放态度。
With parameter binding, you are not only save from SQL injection attacks, but also don't need to worry about format strings for data that actually does not need to be formatted for storage: 使用参数绑定,您不仅可以避免SQL注入攻击,而且不必担心实际上不需要格式化存储的数据的格式化字符串:
using (var connection = new SqlConnection())
{
using (var command = connection.CreateCommand())
{
command.CommandText = "UPDATE " + clsUtility.GetMasterTable() + " SET RECONCILIATION_LAST_UPDATED = :last_updated, CHANGE_DELIVERY_NOTE = :delivery_note";
var parameterLastUpdated = command.CreateParameter();
parameterLastUpdated.ParameterName = ":last_updated";
parameterLastUpdated.SqlDbType = SqlDbType.DateTime;
parameterLastUpdated.Direction = ParameterDirection.Input;
parameterLastUpdated.Value = DateTime.Today;
command.Parameters.Add(parameterLastUpdated);
var parameterDelivery = command.CreateParameter();
parameterDelivery.ParameterName = ":delivery_note";
parameterDelivery.SqlDbType = SqlDbType.NVarChar;
parameterDelivery.Direction = ParameterDirection.Input;
parameterDelivery.Value = "Some string you want in notes";
command.Parameters.Add(parameterDelivery);
command.ExecuteNonQuery();
}
}
Problem is your Date format 问题是您的日期格式
updateQuery = " UPDATE " + clsUtility.GetMasterTable() + " SET ";
updateQuery += " CHANGE_DELIVERY_REASON = 'Card Activation', ";
updateQuery += " CHANGE_DELIVERY_NOTE = '" + notes + "', ";
updateQuery += " RECONCILIATION_STATUS = 1 , ";
updateQuery += " RECONCILIATION_LAST_UPDATED = TO_DATE('" + DateTime.Today + "','DD/MM/YYYY HH:MI:SS AM'), ";
updateQuery += " RECONCILIATION_COUNT = '" + reconciliation_count + "',";
updateQuery += " ACTIVATION_FAILURE = '" + resultPart + "', ";
try this one 试试这个
You should use TO_DATE()
function. 您应该使用
TO_DATE()
函数。
Change your one line to this line: 将您的一行更改为此行:
updateQuery += " RECONCILIATION_LAST_UPDATED = TO_DATE('" +
DateTime.Today + "', 'DD/MM/YYYY HH:MI:SS AM'), ";
Corrected Code: 更正的代码:
updateQuery = " UPDATE " + clsUtility.GetMasterTable() + " SET ";
updateQuery += " CHANGE_DELIVERY_REASON = 'Card Activation', ";
updateQuery += " CHANGE_DELIVERY_NOTE = '" + notes + "', ";
updateQuery += " RECONCILIATION_STATUS = 1 , ";
updateQuery += " RECONCILIATION_LAST_UPDATED = TO_DATE('" + DateTime.Today + "', 'DD/MM/YYYY HH:MI:SS AM'), "; //This Line is changed
updateQuery += " RECONCILIATION_COUNT = '" + reconciliation_count + "',";
updateQuery += " ACTIVATION_FAILURE = '" + resultPart + "', ";
General Syntax: 通用语法:
The syntax for the TO_DATE function in Oracle/PLSQL is: Oracle / PLSQL中TO_DATE函数的语法为:
TO_DATE( string1 [, format_mask] [, nls_language] )
For more info follow the link: 有关更多信息,请单击链接:
https://www.techonthenet.com/oracle/functions/to_date.php
https://www.techonthenet.com/oracle/functions/to_date.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.