英文:
How Do I solve the problem of Oracle deleting data at a specific time?
问题
我的SQL语句如下:
delete from "result" where DATA_DATE < TO_DATE('2023-04-06 00:00:00', 'YYYY-MM-DD HH24-MI-SS')
错误信息是:
ORA-01861: 文本与格式字符串不匹配
我该怎么办?
英文:
My SQL statement is as follows:
delete from "result" where DATA_DATE < TO_DATE('2023-04-06 00:00:00', 'YYYY-MM-DD HH24-MI-SS')
the error message is:
ORA-01861: literal does not match format string
What should I do?
This is part of the information in the table:
enter image description here
答案1
得分: 3
如果DATA_DATE
列存储为DATE
数据类型:
创建表 "result" (data_date DATE);
插入到 "result" (data_date)
值(TO_DATE('2023-04-05 12:34:56', 'YYYY-MM-DD HH24-MI-SS'));
然后你的查询有效:
从 "result" 中删除
其中 DATA_DATE < TO_DATE('2023-04-06 00:00:00', 'YYYY-MM-DD HH24-MI-SS');
影响了 1 行
如果你的DATA_DATE
列是字符串数据类型:
CREATE TABLE "result" (data_date VARCHAR2(19));
INSERT INTO "result" (data_date) VALUES ('2023-04-05 12:34:56');
然后你的查询:
从 "result" 中删除
其中 DATA_DATE < TO_DATE('2023-04-06 00:00:00', 'YYYY-MM-DD HH24-MI-SS');
比较一个字符串和一个日期,Oracle 会隐式地将字符串转换为日期,以便比较相同的数据类型,查询实际上是:
从 "result" 中删除
其中 TO_DATE(
DATA_DATE,
(SELECT value FROM nls_session_parameters WHERE parameter = 'NLS_DATE_FORMAT')
)
< TO_DATE('2023-04-06 00:00:00', 'YYYY-MM-DD HH24-MI-SS');
如果NLS_DATE_FORMAT
会话参数匹配:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
然后查询有效。
但如果会话参数不匹配:
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';
然后可能会出现错误:
ORA-01861: 文字与格式字符串不匹配
解决问题,可以选择:
-
总是将日期存储为
DATE
数据类型(而不是字符串)。 -
不要比较不同数据类型,因为Oracle会执行隐式类型转换,以便比较它们。
-
要么将两边都比较为字符串:
从 "result" 中删除 其中 DATA_DATE < '2023-04-06 00:00:00';
-
要么将两边都比较为日期:
从 "result" 中删除 其中 TO_DATE(data_date, 'YYYY-MM-DD HH24:MI:SS') < TO_DATE('2023-04-06 00:00:00', 'YYYY-MM-DD HH24:MI:SS');
-
-
设置
NLS_DATE_FORMAT
,以便隐式转换正常工作。
虽然这些选项都能起效,但应该选择第一种,修复表,使数据存储在正确的数据类型中。如果实在不能更改表格,那么选择第二种,并确保你的查询比较相同的数据类型(如果需要,可以在类型之间使用显式转换)。选项3只是为了完整起见,它只允许你继续使用依赖隐式类型转换的不良习惯,不要使用它。
英文:
If the DATA_DATE
column is stored as a DATE
data type:
CREATE TABLE "result" (data_date DATE);
INSERT INTO "result" (data_date)
VALUES (TO_DATE('2023-04-05 12:34:56', 'YYYY-MM-DD HH24-MI-SS'));
Then your query works:
delete from "result"
where DATA_DATE < TO_DATE('2023-04-06 00:00:00', 'YYYY-MM-DD HH24-MI-SS');
> lang-none
> 1 rows affected
>
If your DATA_DATE
column is a string data type:
CREATE TABLE "result" (data_date VARCHAR2(19));
INSERT INTO "result" (data_date) VALUES ('2023-04-05 12:34:56');
Then your query:
delete from "result"
where DATA_DATE < TO_DATE('2023-04-06 00:00:00', 'YYYY-MM-DD HH24-MI-SS');
Is comparing a string to a date and Oracle will implicitly convert the string to a date so that it compares the same data types and the query is effectively:
delete from "result"
where TO_DATE(
DATA_DATE,
(SELECT value FROM nls_session_parameters WHERE parameter = 'NLS_DATE_FORMAT')
)
< TO_DATE('2023-04-06 00:00:00', 'YYYY-MM-DD HH24-MI-SS');
If the NLS_DATE_FORMAT
session parameter matches:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Then the query works.
But if the session parameter does not match:
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';
Then you may get the error:
> error
> ORA-01861: literal does not match format string
>
To solve the problem, either:
-
Always store dates in a
DATE
data type (and not as strings). -
Do not compare different data types as Oracle will perform an implicit type conversion so that it can compare them.
-
Either compare both sides as strings:
delete from "result" where DATA_DATE < '2023-04-06 00:00:00';
-
Or compare both sides as dates:
delete from "result" where TO_DATE(data_date, 'YYYY-MM-DD HH24:MI:SS') < TO_DATE('2023-04-06 00:00:00', 'YYYY-MM-DD HH24:MI:SS');
-
-
Set the
NLS_DATE_FORMAT
so that the implicit conversion works.
While any of the options will work; you should use #1 and fix your table so the data is stored in the correct data type. If you really can't change the table then use #2 and ensure that your queries are comparing the same data types (and, if you need to, use explicit conversions between types). Option #3 is just there for completeness and is just going to allow you to continue using bad habits of relying on implicit type conversions; don't use it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论