简体   繁体   English

ORA-01843-无效的Oracle SQL月

[英]ORA-01843 - not a valid month oracle SQL

Below I am attempting to pull information from an oracle database. 下面,我尝试从oracle数据库中提取信息。 However, I keep getting this error: 但是,我不断收到此错误:

Warning: oci_execute(): ORA-01843: not a valid month

Why is my code publishing this error? 为什么我的代码发布此错误? TIA! TIA!

Code: 码:

$objConnect = oci_connect("user", "password", "(description=(address=(protocol=tcp)(host=host)(port=1533))(connect_data=(service_name=sid)))");
$weekSQL2 =  "SELECT * FROM INTOXDM.LSS_COP WHERE COP_WEEK = to_date('2014-08-06 00:00:00', 'yyyy-mm-dd HH24:MI:SS')";
$weekParse2 = oci_parse($objConnect, $weekSQL2);  
$weekExecute2 = oci_execute($weekParse2);
$week2 = oci_fetch_all($weekParse2,$week12);

NOTE 注意

Here is what dates look like in my database: 这是我的数据库中的日期:

2014-06-23 00:00:00.0

Which confuses me even more because my formatting is the exact same as what is in the database. 这让我更加困惑,因为我的格式与数据库中的格式完全相同。

The most likely explanation is that COP_WEEK column is not defined as DATE , but some other datatype (eg VARCHAR), 最可能的解释是COP_WEEK列未定义为DATE ,而是其他一些数据类型(例如VARCHAR),

For the OP query, Oracle is performing an implicit datatype conversion of the values in the COP_WEEK column, from VARCHAR to DATE . 对于OP查询,Oracle正在对COP_WEEK列中的值从VARCHARDATE进行隐式数据类型转换。 The implicit TO_DATE conversion is using format as specified in the client NLS_DATE_FORMAT variable, likely it's the default 'DD-MON-RR' . 隐式TO_DATE转换使用的是客户端NLS_DATE_FORMAT变量中指定的格式,可能是默认的'DD-MON-RR' Whatever it is, it doesn't "match" what's stored in the column. 无论是什么,它都不会“匹配”该列中存储的内容。

Or, if the NLS_DATE_FORMAT does match most of the values stored in the column, there's at least one that has a COP_WEEK value that doesn't match the format. 或者,如果NLS_DATE_FORMAT与存储在该列中的大多数值匹配,则至少有一个COP_WEEK值与格式不匹配。


One code "fix" (I put it in quotes because it's not really the right fix) is to make the conversion explicit, using the TO_DATE function: 一个代码“修复”(我用引号引起来,因为它实际上不是正确的修复)是使用TO_DATE函数使转换显式的:

WHERE TO_DATE(COP_WEEK,'yyyy-mm-dd HH24:MI:SS') = 

Another option for a code "fix" would be to compare the bare VARCHAR column to a VARCHAR literal 代码“修复”的另一个选项是将裸VARCHAR列与VARCHAR文字进行比较

WHERE COP_WEEK = '2014-08-06 00:00:00.0'

This has the advantage of allowing Oracle to make use of an index, rather than requiring a conversion for every row... 这样做的好处是允许Oracle使用索引,而不是要求每一行都进行转换...


A better option would be to store COP_WEEK as a DATE datatype, rather than a VARCHAR, but that would be more than a change to your code. 更好的选择是将COP_WEEK存储为DATE数据类型,而不是VARCHAR,但这将不仅仅是对代码的更改。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM