在 Oracle DB 的 where 子句中使用 Date 数据类型,字符串文字错误
Using Date data type in where clause in Oracle DB, string literal error
所以我从数据库中的列 (columnA) 中提取了这个值:
2014-01-05 00:00:00.0
我想执行一个在 where 子句中使用此值的查询:
WHERE columnA = '2014-01-05 00:00:00.0'
但我收到一个错误:ORA-01861:文字与格式字符串不匹配
我认为这与它是 DATE 数据类型有关;但是我只是从数据库中提取了这个值,所以我知道它的格式正确。我是否仍需要进行某种显式转换才能使其正常工作?
查看NLS_DATE_FORMAT
Oracle 参数。它定义了在转换为日期期间默认应用于字符串文字的格式。不同的 oracle 实例或会话可能有自己的此参数值。默认情况下,此参数的值由服务器区域设置或 NLS_TERRITORY
参数确定。有关详细信息,请参阅 documentation。
例如(SQL加):
test@ORADEV> select current_date from dual;
CURRENT_DA
----------
2015.08.18
test@ORADEV> alter session set NLS_DATE_FORMAT = 'yyyy.dd.MM';
Session altered.
test@ORADEV> select current_date from dual;
CURRENT_DA
----------
2015.18.08
test@ORADEV> alter session set NLS_DATE_FORMAT = 'yyyy.dd.MM hh24';
Session altered.
test@ORADEV> select current_date from dual;
CURRENT_DATE
-------------
2015.18.08 18
TOAD 中的相同查询 returns 18.08.2015 18:31:32
因为 TOAD 有自己的日期格式设置。
你用什么客户端?
再看看下面的例子(SQL再加):
test@ORADEV> set autotrace on trace explain
test@ORADEV> select current_date from dual where current_date <> '2015.18.08 18';
Execution Plan
----------------------------------------------------------
Plan hash value: 4034615273
-----------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-----------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2 (0)| 00:00:01 |
|* 1 | FILTER | | | | |
| 2 | FAST DUAL | | 1 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(CURRENT_DATE<>TO_DATE(' 2015-08-18 18:00:00', 'syyyy-mm-dd hh24:mi:ss'))
Oracle 可以使用自己的日期格式 after literal is converted with NLS_DATE_FORMAT
(syyyy-mm-dd hh24:mi:ss
).
因此,使用精确格式的日期类型显式转换或使用 NLS_DATE_FROMAT
/NLS_TIMESTAMP_FORMAT
(如果对您来说是安全的)。
所以我从数据库中的列 (columnA) 中提取了这个值:
2014-01-05 00:00:00.0
我想执行一个在 where 子句中使用此值的查询:
WHERE columnA = '2014-01-05 00:00:00.0'
但我收到一个错误:ORA-01861:文字与格式字符串不匹配
我认为这与它是 DATE 数据类型有关;但是我只是从数据库中提取了这个值,所以我知道它的格式正确。我是否仍需要进行某种显式转换才能使其正常工作?
查看NLS_DATE_FORMAT
Oracle 参数。它定义了在转换为日期期间默认应用于字符串文字的格式。不同的 oracle 实例或会话可能有自己的此参数值。默认情况下,此参数的值由服务器区域设置或 NLS_TERRITORY
参数确定。有关详细信息,请参阅 documentation。
例如(SQL加):
test@ORADEV> select current_date from dual;
CURRENT_DA
----------
2015.08.18
test@ORADEV> alter session set NLS_DATE_FORMAT = 'yyyy.dd.MM';
Session altered.
test@ORADEV> select current_date from dual;
CURRENT_DA
----------
2015.18.08
test@ORADEV> alter session set NLS_DATE_FORMAT = 'yyyy.dd.MM hh24';
Session altered.
test@ORADEV> select current_date from dual;
CURRENT_DATE
-------------
2015.18.08 18
TOAD 中的相同查询 returns 18.08.2015 18:31:32
因为 TOAD 有自己的日期格式设置。
你用什么客户端?
再看看下面的例子(SQL再加):
test@ORADEV> set autotrace on trace explain
test@ORADEV> select current_date from dual where current_date <> '2015.18.08 18';
Execution Plan
----------------------------------------------------------
Plan hash value: 4034615273
-----------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-----------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2 (0)| 00:00:01 |
|* 1 | FILTER | | | | |
| 2 | FAST DUAL | | 1 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(CURRENT_DATE<>TO_DATE(' 2015-08-18 18:00:00', 'syyyy-mm-dd hh24:mi:ss'))
Oracle 可以使用自己的日期格式 after literal is converted with NLS_DATE_FORMAT
(syyyy-mm-dd hh24:mi:ss
).
因此,使用精确格式的日期类型显式转换或使用 NLS_DATE_FROMAT
/NLS_TIMESTAMP_FORMAT
(如果对您来说是安全的)。