NLS_DATE_FORMAT问题

时间:2016-10-13 08:32:36

标签: sql oracle plsql sqlplus

为什么我在下面看到不同的日期格式:请参阅select语句的输出和匿名块的输出

13:46:23 13:46:23  SQL> conn prashant-mishra/ *****@*****

Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 

13:47:56 13:47:56  SQL> CREATE TABLE test AS SELECT SYSDATE as test_date 
                        FROM dual;
Table created


13:48:15 13:48:15  SQL> SELECT test_date FROM test;
                   TEST_DATE
                   -----------
                   **10/13/2016**

13:50:17 13:50:17  SQL> BEGIN
                   2    FOR rec IN (SELECT test_date FROM test) LOOP
                   3      dbms_output.put_line(rec.test_date);
                   4    END LOOP;
                   5  END;
                   6  /

**13-OCT-16**
PL/SQL procedure successfully completed

其他有用的信息可能是:

13:50:20 13:50:20  SQL> SELECT value FROM v$nls_parameters 
                        WHERE parameter ='NLS_DATE_FORMAT';
            VALUE
            ----------------------------------------------------------------
            DD-MON-RR


13:50:45 13:50:45  SQL> SELECT SYSDATE FROM dual;
            SYSDATE
            -----------
            **10/13/2016**

1 个答案:

答案 0 :(得分:-1)

SQL * Plus使用NLS_DATE_FORMAT将日期格式化为字符串,因此完整的测试用例需要预先显示此参数的内容。您可以尝试以下操作,看看您是否获得一致的格式:

SQL> select value from nls_session_parameters where parameter = 'NLS_DATE_FORMAT';

VALUE
----------------------------------------------------------------
DD-MON-RR

SQL> select sysdate from dual;

SYSDATE
------------------
13-OCT-16

SQL> alter session set nls_date_format = 'YYYY-MM-DD';

Session altered.

SQL> select sysdate from dual;

SYSDATE
----------
2016-10-13

SQL> alter session set nls_date_format = 'DD-MON-RR';

Session altered.

SQL> select sysdate from dual;

SYSDATE
------------------
13-OCT-16

SQL>

根据您的评论获得以下输出:

SQL> select value from nls_session_parameters where parameter = 'NLS_DATE_FORMAT';
VALUE
DD-MON-RR

SQL> select sysdate from dual;
SYSDATE 
10/14/2016

SQL> alter session set nls_date_format = 'YYYY-MM-DD';
Session altered

SQL> select sysdate from dual; 
SYSDATE
10/14/2016

SQL> alter session set nls_date_format = 'DD-MON-RR';
Session altered 

SQL> select sysdate from dual;
SYSDATE 
10/14/2016

所以 - SQLPlus忽略会话NLS_DATE_FORMAT并使用它的唯一本地格式MM/DD/YYYY。我不认为SQL * Plus中存在日期格式覆盖,但可能是错误的。您是否在其他客户端中尝试过相同的命令?