oracle 使用 PL/SQL 将日期值更改为 mm/dd/yyyy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8842299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 03:51:06  来源:igfitidea点击:

Change Date values using PL/SQL to mm/dd/yyyy

oracleplsqloracle11g

提问by PacificNW_Lover

Have some dates in my local Oracle 11g database that are in this format:

在我的本地 Oracle 11g 数据库中有一些采用这种格式的日期:

01-JUL-85

How do I change it to this format (mm/dd/yyyy) using PL/SQL:

如何使用 PL/SQL 将其更改为这种格式 (mm/dd/yyyy):

7/01/1985

With thanks...

感谢...

回答by Ollie

If the column is a date datatype then it is only your NLS_DATEsettings that are causing them to be displayed in the format DD-MON-YYYY.

如果该列是日期数据类型,那么只有您的NLS_DATE设置导致它们以该格式显示DD-MON-YYYY

To check your current NLS_DATE format run the following:

要检查您当前的 NLS_DATE 格式,请运行以下命令:

SELECT value 
  FROM V$NLS_Parameters 
 WHERE parameter ='NLS_DATE_FORMAT';

Oracle stores all dates in an internal binary format and uses the NLS_DATE format to display them (unless explicitly told to display them differently).

Oracle 以内部二进制格式存储所有日期,并使用 NLS_DATE 格式显示它们(除非明确告知以不同方式显示它们)。

You can either alter your NLS_DATEsettings to MM/DD/YYYYor TO_CHARthe date column using:

您可以使用以下方法将NLS_DATE设置更改为MM/DD/YYYYTO_CHAR日期列:

TO_CHAR(<date_column>, 'MM/DD/YYYY')

to see the format you require.

查看您需要的格式。

You can alter the NLS_DATE format for your current session or alter the database parameters to change the default NLS_DATE format for the database itself.

您可以更改当前会话的 NLS_DATE 格式或更改数据库参数以更改数据库本身的默认 NLS_DATE 格式。

If the column is a VARCHAR2 type then you'll need to convert to a date first and then you can format the output using either of the methods described above.

如果该列是 VARCHAR2 类型,那么您需要先转换为日期,然后您可以使用上述任一方法格式化输出。

See: http://ss64.com/ora/syntax-nls.html

见:http: //ss64.com/ora/syntax-nls.html

and: http://www.dba-oracle.com/t_nls_date_format_sysdate.htm

和:http: //www.dba-oracle.com/t_nls_date_format_sysdate.htm

e.g.

例如

SELECT TO_CHAR(sysdate, 'MM/DD/YYYY') as current_date
  FROM dual;

or

或者

ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY';

SELECT sysdate
 FROM dual;

In pure PL/SQL

在纯 PL/SQL 中

DECLARE
   v_date DATE := sysdate;
BEGIN
   DBMS_OUTPUT.put_line(TO_CHAR(v_date, 'MM/DD/YYYY'));
END;