将 24 小时制转换为 12 小时制加上 AM/PM 指示 Oracle SQL

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

Convert 24 Hour time to 12 Hour plus AM/PM indication Oracle SQL

sqloracledatetime

提问by Galen_GG

I am required to do the following as an exercise, and I am struggling to find a solution:

我需要做以下练习,我正在努力寻找解决方案:

Write a SELECT statement that returns these columns from the Invoices table:

编写一个 SELECT 语句,从 Invoices 表中返回这些列:

The invoice_date column

发票日期列

Use the TO_CHAR function to return the invoice_date column with its full date and time including a four-digit year on a 24-hour clock

使用 TO_CHAR 函数返回 invoice_date 列及其完整的日期和时间,包括 24 小时制的四位数年份

Use the TO_CHAR function to return the invoice_date column with its full date and time including a four-digit year on a 12-hour clock with an am/pm indicator.

使用 TO_CHAR 函数返回invoice_date 列及其完整的日期和时间,包括带有 am/pm 指示器的 12 小时制的四位数年份。

Use the CAST function to return the invoice_date column as VARCHAR2(10)

使用 CAST 函数将 invoice_date 列返回为 VARCHAR2(10)

All I can get is:

我所能得到的是:

    select invoice_date, to_char(invoice_date, 'DD-MM-YYYY HH:MM:SS') "Date 24Hr"
    from invoices

Which gets my first two columns, however I can't figure out any way to select the third column. Any help would be great, thanks. (And yes, this is from my school textbook)

这得到了我的前两列,但是我想不出任何方法来选择第三列。任何帮助都会很棒,谢谢。(是的,这是我的学校教科书)

回答by ruakh

For the 24-hour time, you need to use HH24instead of HH.

对于 24 小时制,您需要使用HH24代替HH

For the 12-hour time, the AM/PM indicator is written as A.M.(if you want periods in the result) or AM(if you don't). For example:

对于 12 小时制,AM/PM 指示符写为A.M.(如果您希望结果中包含句点)或AM(如果您不希望)。例如:

SELECT invoice_date,
       TO_CHAR(invoice_date, 'DD-MM-YYYY HH24:MI:SS') "Date 24Hr",
       TO_CHAR(invoice_date, 'DD-MM-YYYY HH:MI:SS AM') "Date 12Hr"
  FROM invoices
;

For more information on the format models you can use with TO_CHARon a date, see http://docs.oracle.com/cd/E16655_01/server.121/e17750/ch4datetime.htm#NLSPG004.

有关可用于TO_CHAR日期的格式模型的更多信息,请参阅http://docs.oracle.com/cd/E16655_01/server.121/e17750/ch4datetime.htm#NLPG004