1:在會話級別設定nls_date_format對應的日期格式。
使用alter session set nls_date_format='xxxx'設定只會影響當前會話的。這個對所有工具(SQL*Plus、SQL Developer)都能生效。
SQL> select sysdate from dual;
SYSDATE
---------
14-SEP-17
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
Session altered.
SQL> select sysdate from dual;
SYSDATE
-------------------
2017-09-14 08:33:17
SQL>
2:如果只是SQL*Plus,可以在SQL*Plus的環境變數設定檔案login.sql或glogin.sql中來設定。
SQL*Plus啟動的時候首先會先執行glogin.sql指令碼,然後查詢當前目錄下是否存在login.sql檔案,如果找到則執行該指令碼,如果當前目錄不存在login.sql.則查詢是否設定了SQLPATH環境變數,找到了就會去執行該環境變數路徑下的login.sql指令碼,否則則會停止繼續查詢。 glogin.sql檔案位於$ORACLE_HOME/sqlplus/admin下.可以在glogin.sql檔案下增加一條語句alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
3: 修改資料庫的引數,需要重啟資料庫後生效
SQL> show parameter nls_date_format;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
nls_date_format string
SQL> alter system set nls_date_format='yyyy-mm-dd hh24:mi:ss' scope=spfile;
System altered.
SQL>
這個是全域性的,有時候影響非常大,所以必須格外小心,要確保不影響各個系統和應用的前提下修改!
4 :修改 Linux 環境變數,在環境變數中設定日期格式。
注意:環境變數NLS_DATE_FORMAT必須和NLS_LANG一起設定,否則不會生效(這個才是總結這篇文章的初衷,以前一直沒有注意這個問題)。可以直接在會話視窗使用export或 .bash_profile 配置檔案(全域性應用)設定。
下面我們來測試一下看看,如下所示:
[oracle@DB-Server ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Wed Sep 13 09:40:48 2017
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select sysdate from dual;
SYSDATE
---------
13-SEP-17
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@DB-Server ~]$ export NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss';
[oracle@DB-Server ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Wed Sep 13 09:41:22 2017
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select sysdate from dual;
SYSDATE
---------
13-SEP-17
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@DB-Server ~]$ export NLS_LANG=AMERICAN
[oracle@DB-Server ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Wed Sep 13 09:41:50 2017
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select sysdate from dual;
SYSDATE
-------------------
2017-09-13 09:41:58
SQL>