按日期筛选select语句时出错

2wnc66cl  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(267)

我从一个同事那里收到了一个脚本,在检查批处理时运行,但是这个脚本在他的笔记本电脑上运行,但是我一在我的设备上运行它,我就得到了一个错误。我不知道它是否可能是权限,因为这是我们设置之间的唯一区别。
该脚本用于选择特定列,将日期格式转换为\u char作为特定的批处理名称,然后筛选特定的日期和时间。以下是示例代码:

select batch_name, NO_OF_ITEMS, NUMBER_OF_ERRORS, to_char(RUN_START_DATE,'DD-MON-YYYY HH24:MI') EXECUTION_START, to_char(RUN_END_DATE,'DD-MON-YYYY HH24:MI') EXECUTION_END,
to_char(START_DATE,'DD-MON-YYYY HH24:MI') START_DATE, to_char(END_DATE,'DD-MON-YYYY HH24:MI') END_DATE, NOTES from bat_log
where BATCH_NAME in (
'SJ63R_03'
) 
and RUN_START_DATE between '04-Aug-2020 07:00' AND '05-Aug-2020 07:00'
order by run_start_date desc;

我得到的错误:

ORA-01830: date format picture ends before converting entire input string
01830. 00000 -  "date format picture ends before converting entire input string"

* Cause:
* Action:

当我去掉时间 07:00RUN_START_DATE between '04-Aug-2020 07:00' AND '05-Aug-2020 07:00' 这个脚本运行得很好,但显然没有过滤时间范围。
如有任何建议,将不胜感激。

dohp0rv5

dohp0rv51#

我试着复制一下你的场景

SQL> create table t ( c1 date , c2 date ) ;

Table created.

SQL> insert into t values ( to_date('01/08/2020 11:00:00','DD/MM/YYYY HH24:MI:SS') , to_date('01/08/2020 17:00:00','DD/MM/YYYY HH24:MI:SS') ) ;

1 row created.

SQL> insert into t values ( to_date('01/09/2020 11:00:00','DD/MM/YYYY HH24:MI:SS') , to_date('01/09/2020 17:00:00','DD/MM/YYYY HH24:MI:SS') ) ;

1 row created.

SQL> select * from t ;

C1        C2
--------- ---------
01-AUG-20 01-AUG-20
01-SEP-20 01-SEP-20

SQL> select c1 , c2 from t where to_char(c1,'DD-MON-YYYY HH24:MI') between '02-AUG-2020 08:00' and '02-SEP-2020 08:00' ;

no rows selected

SQL> select c1 , c2 , to_char(c1,'DD-MON-YYYY HH24:MI') as result from t ;

C1        C2        RESULT
--------- --------- --------------------------
01-AUG-20 01-AUG-20 01-AUG-2020 11:00
01-SEP-20 01-SEP-20 01-SEP-2020 11:00

SQL> select c1 , c2 from t where to_char(c1,'DD-MON-YYYY HH24:MI') between to_date('02-AUG-2020 08:00','DD-MON-YYYY HH24:MI')
  2  and to_date('02-SEP-2020 08:00','DD-MON-YYYY HH24:MI') ;
select c1 , c2 from t where to_char(c1,'DD-MON-YYYY HH24:MI') between to_date('02-AUG-2020 08:00','DD-MON-YYYY HH24:MI')
                            *
ERROR at line 1:
ORA-01830: date format picture ends before converting entire input string

SQL> alter session set nls_date_format='DD-MON-YYYY HH24:MI' ;

Session altered.

SQL> select c1 , c2 from t where c1 between to_date('02-AUG-2020 08:00','DD-MON-YYYY HH24:MI')
and to_date('02-SEP-2020 08:00','DD-MON-YYYY HH24:MI') ;  2

C1                C2
----------------- -----------------
01-SEP-2020 11:00 01-SEP-2020 17:00

SQL>

如果原始字段是日期,则可以更改nls日期设置以使其正常工作,还可以将“日期”应用于要作为日期进行比较的字符串
更新
为了避免隐式转换,我删除了to_字符。

x33g5p2x

x33g5p2x2#

是的,你的两种设置有些不同。它是nls\ U date\ U格式的值。
您的查询依赖于隐式to_date()将字符串“04-aug-2020 07:00”和“05-aug-2020 07:00”从字符串转换为日期,这是一种内部二进制数据类型。因为您依赖于隐式转换,所以您也依赖于nls\u date\u格式的基础值来匹配您提供的字符串。这就是为什么我总是,总是,总是使用to_date()并包含正确的格式掩码:

RUN_START_DATE between to_date('04-Aug-2020 07:00','dd-Mon-yyyy hh24:mi:ss') AND to_date('05-Aug-2020 07:00','dd-Mon-yyyy hh24:mi:ss')

有关更多背景信息,请参见https://edstevensdba.wordpress.com/2011/04/07/nls_date_format/

相关问题