提取源不包含指定的提取字段

问题描述

我正在尝试从Month填充的Year中提取StartDateVARCHAR2我知道那是一个很大的错误,但这不是我的失败),无论我尝试什么,我总是会出错。我注意到问题出在STARTDATE

我有类似这样的查询

SELECT SUM(DAYS) absenceDays
  FROM user_timesheets_absence
 WHERE UserID = 348
   AND EXTRACT(MONTH FROM to_date(StartDate)) = '2020-02-06'
   AND EXTRACT(YEAR FROM to_date(StartDate)) = '2020-02-06'
   AND Approved = 1

我在Stackoverflow上检查了几篇文章,无论我尝试什么,都会收到错误消息

First Post

Second Post

如您所见,我添加了TO_DATE字段,但是我总是收到错误消息

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.

一种表示法也是日期格式为'YYYY-MM-DD'

谁能告诉我我在这里做错了什么?问题出在哪里? VARCHAR2有问题吗?

解决方法

摘录不会返回您发布的内容。

如果startdateyyyy-mm-dd,则使用to_date将其转换为日期,并加上适当的格式掩码。提取返回 month year ,而不是整个“字符串”:

SQL> with test (startdate) as
  2    (select '2020-09-03' from dual)
  3  select to_date(startdate,'yyyy-mm-dd') col1,4    extract(month from (to_date(startdate,'yyyy-mm-dd'))) col2,5    extract(year  from (to_date(startdate,'yyyy-mm-dd'))) col3
  6  from test;

COL1           COL2       COL3
-------- ---------- ----------
03.09.20          9       2020

SQL>

在您的情况下:

and extract(month from to_date(startdate,'yyyy-mm-dd')) = 6
and extract(year  from to_date(startdate,'yyyy-mm-dd')) = 2020

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...