英文:
change database date format from DD-MON-RR to DD/MM/YYYY
问题
我正在尝试永久更改我的Oracle数据库的日期格式,以便以“DD/MM/YYYY”的格式显示日期。目前,日期显示为“24-04-02”,这不是我想要的格式。
我已经尝试使用以下命令:
ALTER SYSTEM set NLS_DATE_FORMAT='DD/MM/YYYY';
ALTER SYSTEM set NLS_DATE_FORMAT='DD/MM/YYYY' SCOPE=SPFILE;
然而,这些命令似乎对日期格式没有任何影响。
我不想使用TO_CHAR(),因为我不是要更改特定查询的格式,也不想使用ALTER SESSION,因为我不想每次启动新会话时都更改格式。
我该怎么做才能永久更改Oracle数据库的日期格式为“DD/MM/YYYY”?
英文:
I'm trying to change the date format of my Oracle database permanently, so that it displays dates in the format "DD/MM/YYYY". Currently, the dates are showing up as "24-04-02", which is not the format I want.
I've already tried using the following commands:
ALTER SYSTEM set NLS_DATE_FORMAT='DD/MM/YYYY';
ALTER SYSTEM set NLS_DATE_FORMAT='DD/MM/YYYY' SCOPE=SPFILE;
However, these commands don't seem to have any effect on the date format.
I don't want to use TO_CHAR() because I'm not trying to change the format for a specific query, and I don't want to use ALTER SESSION because I don't want to have to change the format every time I start a new session.
What can I do to permanently change the date format of my Oracle database to "DD/MM/YYYY"?
答案1
得分: 2
在Oracle中,DATE
是一个由7个字节组成的二进制数据类型,表示世纪、世纪年份、月份、日期、小时、分钟和秒。它始终包含这些组件,从不以任何特定的人类可读格式存储。
您无法更改日期的存储方式。
如果您想查看日期值的二进制表示,请使用:
SELECT DUMP(date_column) FROM table_name;
您所看到的是客户端应用程序(即用于查询数据库的程序)选择将二进制信息显示给您的方式。
一些客户端应用程序(主要是由Oracle编写的,包括SQL*Plus、SQL Developer和SQLcl)将根据NLS_DATE_FORMAT
会话参数设置其默认显示格式。
但是,这仅适用于这些客户端应用程序。在内部,数据库在从字符串到日期的隐式转换以及反之时使用NLS_DATE_FORMAT
作为格式模型。当客户端应用程序请求DATE
数据类型时,数据库会发送日期的未格式化的二进制表示,并让客户端应用程序处理如何格式化它。
其他客户端应用程序将具有自己的内部配置,并将使用它来格式化日期显示。
如果您想以特定方式格式化日期,则可以:
- 从数据库中检索(未格式化的)
DATE
数据类型,然后允许客户端应用程序根据其配置格式化它(如果需要,更新每个客户端应用程序的设置);或者 - 不显示(未格式化的)
DATE
数据类型,而是使用TO_CHAR
(或其他函数)将日期格式化为特定格式。
英文:
In Oracle, a DATE
is a binary data-type that consists of 7 bytes representing century, year-of-century, month, day, hour, minute and second. It ALWAYS has these components and it is NEVER stored in any particular human readable format.
You CANNOT change how dates are stored.
If you want to see the binary representation of the date values then use:
SELECT DUMP(date_column) FROM table_name;
What you are seeing is how the client application (i.e. the program that you use to query the database) choses to display that binary information to you.
Some client applications (i.e. mainly those written by Oracle including SQL*Plus, SQL Developer and SQLcl) will set their default display format according to the NLS_DATE_FORMAT
session parameter.
However, that is specific to those client applications. Internally, the database uses the NLS_DATE_FORMAT
as the format model when implicitly casting from strings to dates and vice-versa. When a client application asks for a DATE
data-type then the database sends the unformatted binary representation of the date and lets the client application handle how that is formatted.
Other client applications will have their own internal configuration and will use that to format dates when displaying them.
If you want to format a date in a specific way then:
- Retrieve (unformatted)
DATE
data-types from the database and allow the client application to format it according to it's configuration (and, if required, update the settings of every client application); or - Don't display an (unformatted)
DATE
data-type and useTO_CHAR
(or other functions) to format the date into a specific format.
答案2
得分: 1
如其他评论所提到的:不要浪费你的时间!数据库的NLS_DATE_FORMAT
只是客户端在未定义客户端日期格式时显示的默认格式。然而,在几乎所有情况下,客户端都会设置NLS_DATE_FORMAT
,因此数据库的NLS_DATE_FORMAT
设置将被忽略。
要配置客户端使用数据库默认格式可能相当困难。对于大多数工具(例如SQL Developer),除非查询数据库的NLS值并将此值应用于ALTER SESSION
命令,否则几乎不可能。
请参阅设置NLS参数:
> 设置NLS参数的方法及其优先级
>
> - 1:(最高)在SQL函数中明确设置
> - 2:通过ALTER SESSION
语句设置
> - 3:作为环境变量设置
> - 4:在初始化参数文件中指定
> - 5:(最低)在创建数据库时指定的默认值
而且,此列表甚至还不完整。对于Windows客户端,您还必须添加来自HKLM\SOFTWARE\ORACLE\KEY_OraClient..
和HKCU\SOFTWARE\ORACLE\KEY_OraClient..
的Registry NLS值。
对于sqlplus,您还需要考虑glogin.sql
和login.sql
等文件。
英文:
As mentioned in other comments: don't waste your time! The database NLS_DATE_FORMAT
is just the default format shown by the client in case nothing is defined at the client. However, in almost every case the client has set NLS_DATE_FORMAT
, so the database NLS_DATE_FORMAT
setting is ignored.
It would be quite difficult to configure a client to use the database default. For most of the tools (e.g. SQL Developer) it is even impossible unless you query the database NLS value and apply this value to an ALTER SESSION
command.
> Methods of Setting NLS Parameters and Their Priorities
>
> - 1: (highest) Explicitly set in SQL functions
> - 2: Set by an ALTER SESSION
statement
> - 3: Set as an environment variable
> - 4: Specified in the initialization parameter file
> - 5: (lowest) Default value specified when the database was created
And this list is even not complete. For a Windows client you also have to add Registry NLS values from HKLM\SOFTWARE\ORACLE\KEY_OraClient..
and HKCU\SOFTWARE\ORACLE\KEY_OraClient..
For sqlplus you have to consider glogin.sql
and login.sql
, and so on.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论