【问题标题】:ORA-01756: quoted string not properly terminated, a part of my query it's failingORA-01756: 引用的字符串未正确终止,我的查询的一部分失败
【发布时间】:2022-01-12 11:21:00
【问题描述】:
select 'BETWEEN TO_DATE(''' || to_char(CURRENT_DATE, 'dd/MM/yyyy 00:00:00') || ''', ''DD/MM/YYYY HH24:MI:SS'') AND TO_DATE(''' || to_char(CURRENT_DATE + 1, 'dd/MM/yyyy 00:00:00')

这个查询有什么不好的地方吗?这不是完整的查询,它只是其中的一部分。我抛出了这个错误:

ORA-01756: quoted string not properly terminated

【问题讨论】:

  • 没有FROM 子句。
  • @jarlh 它只是查询的一部分。我只显示失败部分的错误
  • 我得到 ORA-01821:日期格式无法识别。 IE。没有带引号的字符串问题。

标签: sql oracle


【解决方案1】:

使用 q-quoting 机制。

顺便说一句,你的代码可以简化为

select q'[between trunc(current_date) and trunc(current_date + 1)]' result
from dual;

不需要to_char 然后to_date current_date;它已经 DATE 数据类型,只需删除(截断到午夜)时间组件。


顺便说一句#2,你使用的格式掩码是错误的;应该是dd/mm/yyyy hh24:mi:ss(不是dd/mm/yyyy 00:00:00


如果你坚持(不过,我不知道你为什么会这样),那么

select q'[between to_date(to_char(current_date    , 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss')]' ||
       q'[    and to_date(to_char(current_date + 1, 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss')]'
    as result 
from dual;        

有效吗?是的:

SQL> select q'[between to_date(to_char(current_date    , 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss')]' ||
  2         q'[    and to_date(to_char(current_date + 1, 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss')]'
  3      as result
  4  from dual;

RESULT
--------------------------------------------------------------------------------
between to_date(to_char(current_date    , 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy
hh24:mi:ss')    and to_date(to_char(current_date + 1, 'dd/mm/yyyy hh24:mi:ss'),
'dd/mm/yyyy hh24:mi:ss')


SQL>

现在将结果复制/粘贴到另一个查询中并验证它:

SQL> select *
  2  from dual
  3  where sysdate
  4  -- this is the "result":
  5  between to_date(to_char(current_date    , 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss')    and to_date(to_char(current_date + 1, 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss')
  6  ;

D
-
X

SQL>

没有失败,嗯?

【讨论】:

  • 为什么忽略 "dd/MM/yyyy 00:00:00" 的部分?在我的查询中,我只想要一天再加一天。也许我错了,只是问
  • 我告诉过你 - 这是 invalid 格式掩码。使用我建议的第一个代码:between trunc(current_date) and trunc(current_date + 1)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 2020-12-27
  • 1970-01-01
相关资源
最近更新 更多