问题描述
我正在尝试从Month
填充的Year
中提取StartDate
和VARCHAR2
(我知道那是一个很大的错误,但这不是我的失败),无论我尝试什么,我总是会出错。我注意到问题出在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上检查了几篇文章,无论我尝试什么,都会收到错误消息
如您所见,我添加了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
有问题吗?
解决方法
摘录不会返回您发布的内容。
如果startdate
是yyyy-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