1

我在将日期转换为 varchar2 类型时遇到问题,如下例所示:

TABLE TEST

Name  Type          Nullable Default Comments 
----- ------------- -------- ------- -------- 
DESCR VARCHAR2(100) Y                         
DATA  DATE          Y                         
DATA2 VARCHAR2(500) Y  


execute immediate 'ALTER SESSION SET NLS_DATE_FORMAT = ''DD/MM/YYYY HH:MI:SS AM''';
DELETE FROM TEST;
INSERT INTO TEST SELECT 'SOME TEXT', SYSDATE, TO_CHAR(SYSDATE,'DD/MM/YYYY HH:MI:SS AM') FROM DUAL;   


1 SOME TEXT "19/03/2015 12:05:45" "28/12/1393 12:05:45 TARDE"

在表中插入 SYSDATE 转换 to_char 到 varchar2 字段时,日期为28/12/1393.

4

2 回答 2

0

以下对我来说可以(在 11.2.0.2 上,使用客户端 11.2.0.2):

column descr format a12
column data2 format a30

create table test (descr varchar2(100),
                   data date,
                   data2 varchar2(500));

begin
  execute immediate 'ALTER SESSION SET NLS_DATE_FORMAT = ''DD/MM/YYYY HH:MI:SS AM''';
end;
/

DELETE FROM TEST;

INSERT INTO TEST SELECT 'SOME TEXT', SYSDATE, TO_CHAR(SYSDATE,'DD/MM/YYYY HH:MI:SS AM') FROM DUAL;   

commit;

select * from test;

drop table test;

选择返回:

DESCR        DATA                   DATA2                         
------------ ---------------------- ------------------------------
SOME TEXT    19/03/2015 12:17:00 PM 19/03/2015 12:17:00 PM        

您能否运行相同的测试用例并确认 select 确实返回不正确?

另外,您的 Oracle 数据库版本是什么,您的 Oracle 客户端版本是什么?

ETA:突然想到...您的表上没有更新 varchar2 列中日期的触发器,是吗?

于 2015-03-19T16:22:24.303 回答
0

我将它作为一项工作运行,所以我对您的脚本进行了一些更改:

create table test (descr varchar2(100),
               data date,
               data2 varchar2(500));

create procedure testt is
begin
execute immediate 'ALTER SESSION SET NLS_DATE_FORMAT = ''DD/MM/YYYY HH:MI:SS AM''';


DELETE FROM TEST;

 INSERT INTO TEST SELECT 'SOME TEXT', SYSDATE, TO_CHAR(SYSDATE,'DD/MM/YYYY HH:MI:SS AM') FROM DUAL;  

commit;

end;
/
declare
job number;
begin
DBMS_JOB.Submit(
                job => JOB,
                what => 'testt();',
                next_date => SYSDATE
                );
                commit;
end;
/

SELECT * FROM TEST;

DROP TABLE TEST;
DROP PROCEDURE TESTT;

我得到了结果:

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0

Connected as 

SQL>

SQL> create table test (descr varchar2(100),

 data date,

 data2 varchar2(500));

Table created

SQL> create procedure testt is

 begin

 execute immediate 'ALTER SESSION SET NLS_DATE_FORMAT = ''DD/MM/YYYY HH:MI:SS AM''';

 DELETE FROM TEST;

 INSERT INTO TEST SELECT 'SOME TEXT', SYSDATE, TO_CHAR(SYSDATE,'DD/MM/YYYY HH:MI:SS AM') FROM DUAL;

 commit;

 end;

16 /

Procedure created

SQL> declare

 job number;

 begin

 DBMS_JOB.Submit(

 job => JOB,

 what => 'testt();',

 next_date => SYSDATE

 );

 commit;

end;

/

PL/SQL procedure successfully completed

   SQL> SELECT * FROM TEST;

    DESCR DATA DATA2

    -------------------------------------------------------------------------------- ----------- --------------------------------------------------------------------------------

    SOME TEXT 19/03/2015 28/12/1393 01:47:57 TARDE
于 2015-03-19T16:59:55.627 回答