简体   繁体   English

如何解决SQL错误:ORA-01843:无效月份

[英]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.

相关问题 如何将字符串类型查询的特定部分转换为最新日期,以避免出现“ ORA-01843:无效月份”错误? - How can I cast specific part of string type query to date to avoid “ORA-01843: not a valid month” error? ORA-01843:无效月份-在第二台PC上发行 - ORA-01843: not a valid month - issue on second pc 插入表中会返回ORA-01843:无效月份 - insert in to table gives back ORA-01843: not a valid month 在SELECT中计算日期时得到“ ORA-01843:无效的月份” - Getting “ORA-01843: not a valid month” when calculating dates in SELECT Oracle.ManagedDataAccess.Client.OracleException:“ORA-01843:无效月份” - Oracle.ManagedDataAccess.Client.OracleException: "ORA-01843: not a valid month" 为什么将datetime转换为字符串会导致错误:ORA-01843:无效的月份 - Why converting datetime to string gets me the error : ORA-01843: not a valid month ORA-01843:无效月份-在DB上工作,但在asp.net网页上进行相同操作时却无效 - ORA-01843: not a valid month — working on DB but not when doing the same on asp.net web pages 当我尝试向Oracle插入日期和时间时得到ORA-01843 - got ORA-01843 when I try to insert date & time to Oracle 如何解决我的个人ORA-12154错误? - How can I solve my personal ORA-12154 error? 如何解决此错误:必须在字符串中指定有效信息以进行解析 - How to solve this error : must specify valid information for parsing in the string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM