Skip Characters in Oracle TO_DATE function

家住魔仙堡 提交于 2019-12-23 21:26:07

问题


I'm importing data that has SQL Server formatted dates in a .tsv (yyyy-mm-dd hh24:mi:ss.mmm) into an Oracle database using SQL Developer's Import Data wizard.

How can I ignore the .mmm characters for importing them into a DATE column? I cannot seem to find an answer to this; I get that DATE columns don't hold milliseconds, but why can't I ignore specific patterns in the TO_DATE call?

Also note that because I'm not generating the TO_DATE calls, I cannot SUBSTRING or otherwise manipulate the .tsv's value during the import.


回答1:


You can use the pound sign (#) for this:

SELECT TO_DATE('2015-01-01 01:00:00.999', 'yyyy-mm-dd hh24:mi:ss.###') FROM dual;
--> 01/01/2015 01:00:00

I did not find this in the documentation, so I can't say why, but these also work:

SELECT TO_DATE('01_','hh24#') FROM dual;

SELECT TO_DATE('01:01:01.0xx','hh24:mi:ss.###') FROM dual;

These don't seem to work:

SELECT TO_DATE('010','hh24#') FROM dual;

SELECT TO_DATE('01:01:01.xxx','hh24:mi:ss.###') FROM dual;

This seems to be undocumented, but you seem to be able to interchange the punctuations without problems:

SELECT TO_DATE('2015-01;01 11:12/13',
               'yyyy.mm,dd_hh-mi ss') FROM dual;
--> 01/01/2015 11:12:13

You can use Character literals, enclosed in double quotation marks if you know which text to ignore:

SELECT TO_DATE('foo2015bar-01-!#%}01', '"foo"yyyy"bar"-mm-"!#%}"dd') FROM dual;
--> 01/01/2015



回答2:


There's no need to do this; Oracle will automagically convert a timestamp into a date if you attempt to add fractional seconds to a date column. Use TO_TIMESTAMP() instead and embrace the fractional seconds.

SQL> create table tmp_test (dt date);

Table created.

SQL> insert into tmp_test
  2  select to_timestamp('2015-03-24 13:10:03.654','yyyy-mm-dd hh24:mi:ss.FF3')
  3    from dual;

1 row created.

SQL> alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';

Session altered.

SQL> alter session set nls_timestamp_format = 'yyyy-mm-dd hh24:mi:ss.FF3';

Session altered.

SQL> select * from tmp_test;

DT
-------------------
2015-03-24 13:10:03

SQL>


来源:https://stackoverflow.com/questions/29229622/skip-characters-in-oracle-to-date-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!