【问题标题】:oracle sql time comparisonoracle sql时间比较
【发布时间】:2021-02-09 01:45:26
【问题描述】:

我有这个 oracle 表和以下数据:

CREATE TABLE data( 
 fielda DATE DEFAULT sysdate, //sample data: 06/02/2021
  fieldb TIMESTAMP(6) WITH LOCAL TIME ZONE DEFAULT current_timestamp //sample data:05/02/2021 11:44:04.000000000
);

我想列出表数据中的所有记录,其中timea不等于timeb。

我写了这个查询:

select * from  data where TO_DATE(timea, 'dd/mm/yyyy') != TO_DATE(timea, 'dd/MM/yyyy hh24:mi:ss') ;

但我得到错误: ORA-01830: date format picture ends before converting entire input string

请告知如何列出表 a 中的所有记录,其中 timea 不等于 timeb。

【问题讨论】:

  • 列的数据类型有哪些?
  • 请编辑问题并显示表格定义和准确的示例格式
  • 使用示例数据、字段类型更新问题
  • 您的表名与查询中的不匹配。 mt_mdate、mt_ptime 在哪里?请显示表的完整 CREATE TABLE 语句。
  • 还添加了 sql 创建模式

标签: sql oracle11g


【解决方案1】:

oracle 基本数据类型:日期和时间戳是两种不同的数据类型。最后一个更精确。 您的 to_date 转换似乎很连贯。 to_date 将字符串(文本格式)转换为日期。 根据您的表格定义, fielda 是日期类型。为什么要转换?什么是参数timea? to_date(timea,...) != to_date(timea,...) 你的意思是什么? 支持你尝试查询一些与指定日期不匹配的记录,where子句应该像“ where sometable.somefiled != to_date('specified_date_string_with_format','specified_format')”

【讨论】:

  • fielda 和 fieldb 都包含时间。我想列出 fielda 和 fieldb 不匹配的记录
  • 您的代码明显显示的不是原始代码。参数 timea 似乎很连贯。如果要将日期类型与指定的日期值进行比较,则等号的正确值应该是日期类型值。或者你像“ fielda !=to_date('2021-02-09 10:00:00','yyyy-mm-dd HH24:MI:SS) ”一样进行转换
猜你喜欢
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 2017-06-24
  • 2012-09-15
  • 2018-08-31
相关资源
最近更新 更多