[英]Comparing Date field with specific datetime in oracle
I have a table with a Date field time_process
like '1/11/2021 5:10:16 AM'
.我有一个带有日期字段time_process
的表,例如'1/11/2021 5:10:16 AM'
。
I want to select records has time_process >= '1/11/2021 05:00:00 AM'
But when I query:我想 select 记录有time_process >= '1/11/2021 05:00:00 AM'
但是当我查询时:
select * from DATA_RUN where time_process >= '1/11/2021 05:00:00 AM'
Has error:有错误:
[1]: ORA-01843: not a valid month
How can I compare this Date field with specific datetime?如何将此日期字段与特定日期时间进行比较?
You should use to_date
to convert string to date as follows:您应该使用to_date
将字符串转换为日期,如下所示:
select * from DATA_RUN
where time_process >= To_date('01/11/2021 05:00:00 AM', 'mm/dd/yyyy hh:mi:ss AM')
If this is a DATE
datatype column, don't compare it to a string (because, this: '1/11/2021 05:00:00 AM'
is a string, not a date).如果这是DATE
数据类型列,请不要将其与字符串进行比较(因为,this: '1/11/2021 05:00:00 AM'
是字符串,而不是日期)。
So:所以:
select *
from data_run
where time_process >= to_date('11.01.2021 05:00:00', 'dd.mm.yyyy hh24:mi:ss');
SELECT *
FROM DATA_RUN
WHERE time_process >= DATE '2021-01-11' + INTERVAL '05:00:00' HOUR TO SECOND
Or, since you don't have any minutes or seconds, then you could use:或者,由于您没有任何分钟或秒数,那么您可以使用:
WHERE time_process >= DATE '2021-01-11' + INTERVAL '5' HOUR
SELECT *
FROM DATA_RUN
WHERE time_process >= TIMESTAMP '2021-01-11 05:00:00'
TO_DATE
function:选项 3 - 使用TO_DATE
function 转换字符串:SELECT *
FROM DATA_RUN
WHERE time_process >= TO_DATE( '1/11/2021 05:00:00 AM', 'MM/DD/YYYY HH12:MI:SS AM' )
As an aside,作为旁白,
I have a table with a Date field
time_process
like'1/11/2021 5:10:16 AM'
我有一个带有日期字段time_process
的表,例如'1/11/2021 5:10:16 AM'
A DATE
column has no format as it is stored internally in a binary format consisting of 7-bytes (century, year-of-century, month, day, hour, minute and second). DATE
列没有格式,因为它以由7 个字节(世纪、世纪年、月、日、小时、分钟和秒)组成的二进制格式在内部存储。 Formatting the value of a DATE
is left up to the user interface;格式化DATE
的值由用户界面决定; for SQL/Plus and SQL Developer (among others), this is determined by the NLS_DATE_FORMAT
session parameter but each user can set their own values in each of their sessions at any time so you cannot assume that there will be a consistent format to a DATE
column.对于 SQL/Plus 和 SQL 开发人员(以及其他),这由NLS_DATE_FORMAT
session 参数确定,但每个用户可以随时在每个会话中设置自己的值,因此您不能假设DATE
会有一致的格式柱子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.