Oracle 格式字符串没有';不匹配

Oracle 格式字符串没有';不匹配,oracle,plsql,Oracle,Plsql,我的查询中有一个问题,我不知道问题在哪里。所以问题是,当我运行查询时,会出现如下错误 ORA-01861: literal does not match format string 01861. 00000 - "literal does not match format string" *Cause: Literals in the input must be the same length as literals in the format

我的查询中有一个问题,我不知道问题在哪里。所以问题是,当我运行查询时,会出现如下错误

ORA-01861: literal does not match format string
01861. 00000 -  "literal does not match format string"
*Cause:    Literals in the input must be the same length as literals in
           the format string (with the exception of leading whitespace).  If the
           "FX" modifier has been toggled on, the literal must match exactly,
           with no extra whitespace.
*Action:   Correct the format string to match the literal.
到目前为止,我的问题就在这里

SELECT rp.* 
FROM responsible_persons  rp
LEFT JOIN projects  p ON p.ProjectID = rp.ProjectID
WHERE rp.UserID = 195
AND (SYSDATE BETWEEN p.StartDate AND p.EndDate)
到目前为止,我试图将StartDate和EndDate
转换为_date()
,但仍然存在问题。
这里怎么了?我哪里出错了?

尝试将会话中的NLS\u DATE\u格式设置为表中的值,该sysdate可以获得与表中数据相同的格式:

ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD' ;

SELECT rp.* 
FROM responsible_persons  rp
LEFT JOIN projects  p ON p.ProjectID = rp.ProjectID
WHERE rp.UserID = 195
AND (SYSDATE BETWEEN p.StartDate AND p.EndDate)
一个例子

SQL> create table t ( c1 varchar2(20) , c2 varchar2(20) ) ;

Table created.

SQL> insert into t values ( '22.01.2020 10:00:00' , '22.01.2020 21:00:00' ) ;

1 row created.

SQL> select * from t where ( sysdate between c1 and c2 ) ;
select * from t where ( sysdate between c1 and c2 )
                                        *
ERROR at line 1:
ORA-01843: not a valid month


SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss'  ;

Session altered.

SQL> select * from t where ( sysdate between c1 and c2 ) ;

no rows selected

SQL>

您真的不应该将日期存储为字符串。 话虽如此,您还是希望将代码更改为

AND (SYSDATE BETWEEN TO_DATE(p.StartDate,'YYYY-MM-DD') AND TO_DATE(p.EndDate,'YYYY-MM-DD'))
但是要了解,如果p.EndDate是'2020-08-28',SYSDATE是2020-08-28 07:49:30,那么您将无法获得匹配,因为您的日期字符串不包含SYSDATE包含的时间组件。因此,您可能还希望使用此命令从SYSDATE中删除时间组件

TRUNC(SYSDATE)

正如Roberto所建议的,您可以操纵会话的NLS_DATE_格式来解决此问题。但是,如果您将其他日期存储为字符串,并且它们的格式不相同,那么您就把另一个问题推到了堆栈上。

开始日期和结束日期的数据类型是什么,这些值是什么样子的呢?ştartDate和endDate是varchar2 varchar2列
startDate
endDate
中的数据格式是什么?格式是“YYYY-MM-DD”。其他人已经为您眼前的问题提供了合法的解决方案,但请允许我指出(正如其他人所提到的)将日期存储为VARCHAR2是一个非常严重的设计缺陷。这应该得到纠正。如果你说“但这是一个现有的系统”,我会回答“那么你现有的系统有一个严重的bug需要修复,就像你发现的其他严重bug一样”。