2016-10-13 46 views
2

爲什麼我看到下面不同的日期格式:請參見SELECT語句和匿名塊的輸出的輸出問題與NLS_DATE_FORMAT

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** 
+0

匿名塊可能會使用與SQL * Plus會話不同的NLS設置(dbms_output調用中的隱式TO_CHAR轉換)。請檢查'nls_session_parameters'和'nls_database_parameters'以瞭解可能解釋您所看到行爲的任何差異。 –

+0

@MartinSchapendonk:我沒有看到任何區別。 select * from NLS_DATABASE_PARAMETERS WHERE parameter ='NLS_DATE_FORMAT'; - DD-MON-RR SELECT * FROM NLS_SESSION_PARAMETERS WHERE parameter ='NLS_DATE_FORMAT'; - DD-MON-RR –

+0

當您明確地更改匿名塊的輸出時會話*在執行之前使用'alter session set NLS_DATE_FORMAT ='DD-MON-RR';'? –

回答

-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中有日期格式覆蓋,但可能是錯誤的。你有沒有在不同的客戶端嘗試相同的命令?

+0

13:05:38 13:05:38 SQL>從nls_session_parameters中選擇值其中parameter ='NLS_DATE_FORMAT'; VALUE DD-MON-RR 13:05:50 13:05:50 SQL>從dual中選擇sysdate; SYSDATE 10/14/2016 13:05:56 13:05:56 SQL> alter session set nls_date_format ='YYYY-MM-DD'; 會話已更改 13:06:04 13:06:04 SQL> select dual from sysdate; SYSDATE 10/14/2016 13:06:08 13:06:08 SQL> alter session set nls_date_format ='DD-MON-RR'; 會話已更改 13:06:20 13:06:20 SQL>從雙重選擇sysdate; SYSDATE 10/14/2016 –

+0

Downvote看起來有點苛刻。 –