ora-01861:文字与格式字符串不匹配解决方案

时间:2025-05-13 08:01:50

前言

        按照对接文档,提供的时间字段是字符串,将字符串转换成时间戳返回:

SQL:select guid, puc_id, system_id, org_identifier, org_alias, org_code, enable_flag, update_time from typppbd.view_puc_organization where update_time is not null order by to_date(update_time, 'yyyy-MM-dd hh24:mi:ss') desc

执行出错,结果如上

 

问题原因

            实际上数据库的update_time字段是DATE类型的,所以上述的写法是错误的

 

正确的写法

select guid, puc_id, system_id, org_identifier, org_alias, org_code, enable_flag, to_char(update_time, 'yyyy-mm-dd hh24:mi:ss') from typppbd.view_puc_organization where update_time is not null order by update_time desc

这个时候,采用to_char将时间类型的数据转换成字符串,否则,通过OTL C++组件访问的时候,抛异常

 

引申

ORACLE已经在DATE数据类型上扩展出来了TIMESTAMP数据类型,它包括了所有DATE数据类型的年月日时分秒的信息,而且包括了小数秒的信息

oracle数据库的timestamp的存储精度为6位,即微秒级

针对date和timestamp需要通过转换成字符串的方式才能通过otl获取到

select dev_no, longitude, latitude, to_char(t.date_time, 'yyyy-mm-dd hh24:mi:ss') 

from typppbd.view_pdt_trace t 

where t.date_time > sysdate + numtodsinterval(-7200, 'second') 

order by t.date_time asc

date转换成字符串:2020-11-14 17:18:48

select GPS_NO, LATITUDE, LONGITUDE, to_char(REPORT_TIME, 'yyyy-mm-dd hh24:mi:ss.ff6'), ONLINE_STATUS 

from big.t_MAG_GPS_JC_INFO

timestamp转换成字符串:2020-11-14 17:18:48.761000

 

总结

        编写SQL语句的时候,必须将现场数据库表的字段和数据导出来,进行检验,文档不一定靠谱,因为默认情况下,认为日期是标准格式:2020-10-10 10:10:10

导出SQL语句:INSERT INTO "" ("GPS_NO" ,"UPDATE_TIME"") VALUES ('33059143001310993033', NULL, NULL, TO_TIMESTAMP('20210607163639554000', TO_TIMESTAMP('20210607163639554000', );

TO_TIMESTAMP说明在数据库表中字段是timestamp类型