使用 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>
没有失败,嗯?