SQL 如何以“DD MON YYYY”格式显示日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24444971/
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-01 02:07:45  来源:igfitidea点击:

How do I display DATE in 'DD MON YYYY' format?

sqloracle

提问by user3781895

I am a newbie for Oracle database programming and I wish to INSERT date (also display) in 'DD MON YYYY' format. (PS: This only involves INSERT event). Which data type (DATE or TIMESTAMP) is the most suitable option for me in order to accomplish this format? How was I supposed to do that? Thanks.

我是 Oracle 数据库编程的新手,我希望以“DD MON YYYY”格式插入日期(也显示)。(PS:这里只涉及 INSERT 事件)。为了完成这种格式,哪种数据类型(DATE 或 TIMESTAMP)最适合我?我该怎么做?谢谢。

回答by a_horse_with_no_name

A DATEcolumn does not have any format.

DATE列没有任何格式。

So the format that you use when inserting or updating data is irrelevant for displaying that data (that's one of the reasons why you should never store a date in a VARCHARcolumn).

因此,您在插入或更新数据时使用的格式与显示该数据无关(这是您永远不应该在VARCHAR列中存储日期的原因之一)。

Any formatted output you see for a DATEcolumn in your SQL tool (e.g. SQL*Plus) is applied by that tool. It is not part of the data stored in that column.

DATE在 SQL 工具(例如 SQL*Plus)中看到的列的任何格式化输出都由该工具应用。它不是存储在该列中的数据的一部分。

When providing a date literal you should either use the to_date()function with an explicit format mask:

在提供日期文字时,您应该使用to_date()带有显式格式掩码的函数:

insert into some_table (some_date_column)
values (to_date('27-06-2014', 'dd-mm-yyyy'));

I also do not recommend using formats with written month names (27-JUN-2014) when supplying a date literal because they alsodepend on the NLS settings of the client computer and might produce (random) errors due to different languages. Using numbers only is much more robust.

我也不建议在提供日期文字时使用带有书面月份名称 (27-JUN-2014) 的格式,因为它们取决于客户端计算机的 NLS 设置,并且可能会因不同的语言而产生(随机)错误。仅使用数字要健壮得多。

I prefer to use ANSI date literals because it's a bit less typing:

我更喜欢使用 ANSI 日期文字,因为它的输入要少一些:

insert into some_table (some_date_column)
values (DATE '2014-06-27');

The format for an ANSI date (or timestamp) literal is alwaysthe ISO format (yyyy-mm-dd).

ANSI 日期(或时间戳)文字的格式始终是 ISO 格式 (yyyy-mm-dd)。

When you select your data you can display the date in whatever format you like. Either by using the to_char()function (e.g. when using a SQL tool) or using functions from your programming language (the preferred way to use inside an application):

当您选择数据时,您可以以您喜欢的任何格式显示日期。通过使用to_char()函数(例如使用 SQL 工具时)或使用您的编程语言中的函数(在应用程序内部使用的首选方式):

select to_char(some_date_column,'dd-mon-yyyy')
from some_table;

Note that the DATEdata type in Oracle (despite it's name) also stores the time. a TIMESTAMPdoes the same thing only with a higher precision (it includes milliseconds, whereas the DATEdata type only stores seconds).

请注意,DATEOracle中的数据类型(尽管它的名称)也存储时间。aTIMESTAMP只以更高的精度做同样的事情(它包括毫秒,而DATE数据类型只存储秒)。

回答by Nishanthi Grashia

To inserta record,

插入记录,

INSERT INTO TABLE_NAME (DATE_FIELD) VALUES (TO_DATE ('27-JUN-2014', 'DD-MON-YYYY');

It is advisable to use DATE data-type until and unless you need the date's accuracy to be till milli seconds. In your case, go with DATE datatype and TIMESTAMP is not necessary

建议使用 DATE 数据类型直到并且除非您需要日期的准确性直到毫秒。在您的情况下,使用 DATE 数据类型并且不需要 TIMESTAMP

To selecta record,

选择记录,

SELECT TO_CHAR(DATE_FIELD, 'DD-MON-YYYY') FROM TABLE_NAME;

In genral, remember this:

一般来说,记住这一点:

  • TO_DATEis a function used to convert a string(CHAR) TO DATE
  • TO_CHARis a function used to convert a DATE to a string(CHAR)
  • TO_DATE是用于将字符串(CHAR)转换为日期的函数
  • TO_CHAR是用于将 DATE 转换为字符串(CHAR)的函数

回答by Ankit Bajpai

In this scenario date datatype will be suitable for you, and for the desired format you should try like this:-

在这种情况下,日期数据类型将适合您,对于所需的格式,您应该像这样尝试:-

 INSERT INTO TABLE_NAME(DATE_COLUMN) VALUES('27-JUN-2014');

Hope this can help you.

希望这可以帮到你。