oracle 当一列实际上是日期而其他列只是时间时,如何显示DATE数据类型的所有列?

50few1ms  于 2022-11-28  发布在  Oracle
关注(0)|答案(3)|浏览(110)

我正在学习SQL并使用Oracle SQL Developer。我有一个包含以下列的表
1.航班日期
1.出发时间
1.到达时间
我使用以下方法插入了值

TO_DATE('10:45', 'hh24:mi')

TO_DATE('20/10/2000', 'DD/MM/YYYY')

当我执行SELECT * FROM TABLE_NAME时,DepartureTime和ArrivalTime会显示日期(我尚未输入)。如何在第一栏显示日期,在其他两栏显示时间?
我试过了

SELECT to_char(DepartureTime, 'HH24:MI' ) AS Departure
        to_char( ArrivalTime, 'HH24:MI' ) AS Arrival
  FROM FLIGHT;

'虽然上面的语句显示了正确的值,但我想写一条语句来输出所有列(因为实际的表有3列以上),但要采用上面解释的格式-FlightDate为日期,DepartureTime和ArrivalTime为时间。

uqcuzwp8

uqcuzwp81#

在Oracle中,DATE是一种二进制数据类型,由7个字节组成,代表世纪、世纪年份、月、日、小时、分钟和秒。它总是具有所有这些组件,并且从不以任何特定(人类可读)格式存储。
因此,如果您有一个DATE,它将始终既是日期又是时间。
如果只想存储日期的日期部分,则需要使用整个日期,但可以添加一个约束以确保时间始终为午夜。
如果要存储不带日期的时间,则可以:
1.使用DATE数据类型,只设置时间组件(忽略日期组件的默认值);或
1.使用INTERVAL DAY TO SECOND数据类型。
例如,您的数据表可以是:

CREATE TABLE table_name (
  FlightDate    DATE
                CONSTRAINT table_name__flightdate__chk CHECK (flightdate = TRUNC(flightdate)),
  DepartureTime INTERVAL DAY(0) TO SECOND(0) NOT NULL,
  ArrivalTime   INTERVAL DAY(1) TO SECOND(0) NOT NULL
);

或者,您可以将表格简化为:

CREATE TABLE table_name (
  Departure DATE NOT NULL,
  Arrival   DATE NOT NULL
);

而不用担心有不同的日期和时间。

nbysray5

nbysray52#

一种选择是更改会话的NLS,并同时影响 * 所有 * DATE数据类型列。
样表及部分数据:

SQL> create table flight
  2    (id             number,
  3     flight_date    date,
  4     departure_time date,
  5     arrival_time   date);

Table created.

SQL> insert into flight values (1, to_date('10:45', 'hh24:mi'), to_date('20/10/2000', 'dd/mm/yyyy'), sysdate);

1 row created.

以下是my数据库返回的结果:

SQL> select * From flight;

        ID FLIGHT_DA DEPARTURE ARRIVAL_T
---------- --------- --------- ---------
         1 01-NOV-22 20-OCT-00 24-NOV-22

现在,将格式修改为其他格式:

SQL> alter session set nls_date_Format = 'dd.mm.yyyy hh24:Mi';

Session altered.

结果:

SQL> select * From flight;

        ID FLIGHT_DATE      DEPARTURE_TIME   ARRIVAL_TIME
---------- ---------------- ---------------- ----------------
         1 01.11.2022 10:45 20.10.2000 00:00 24.11.2022 21:17

SQL>

这是一个SQL*Plus示例,但在SQL Developer中同样适用。
或者,打开SQL Developer的首选项(在“工具”菜单中),搜索“NLS”并输入所需的日期格式

e37o9pze

e37o9pze3#

The question "When I do a SELECT * FROM TABLE_NAME, the DepartureTime and ArrivalTime display a date (which I have not entered). How do I display the date in the first column and time in the other 2 columns?" as I understand tells that DepartureTime and ArrivalTime columns are populated with date and time (as it is quite usual). I assume that your data looks something like here:

WITH
    flights (FLIGHT_OPERATOR, FLIGHT_FROM, FLIGHT_TO, DEPARTURE, ARRIVAL, SOME_OTHER_COL) AS
        (
            Select 'Vueling', 'Barcelona', 'Paris', To_Date('01.11.2022 10:45', 'dd.mm.yyyy hh24:mi'),   To_Date('01.11.2022 12:30', 'dd.mm.yyyy hh24:mi'), 'Some other stuff' From Dual Union All
            Select 'RyanAir', 'Barcelona', 'Dublin', To_Date('01.11.2022 11:10', 'dd.mm.yyyy hh24:mi'),   To_Date('01.11.2022 13:00', 'dd.mm.yyyy hh24:mi'), 'Some other stuff' From Dual Union All
            Select 'KLM', 'Barcelona', 'Amsterdam', To_Date('01.11.2022 20:10', 'dd.mm.yyyy hh24:mi'),   To_Date('01.11.2022 23:00', 'dd.mm.yyyy hh24:mi'), 'Some other stuff' From Dual Union All
            Select 'Lufthansa', 'Barcelona', 'Frankfurt', To_Date('01.11.2022 23:25', 'dd.mm.yyyy hh24:mi'),   To_Date('02.11.2022 02:20', 'dd.mm.yyyy hh24:mi'), 'Some other stuff' From Dual 
        )

Selecting all from table looks, probably, like below:
| FLIGHT_OPERATOR | FLIGHT_FROM | FLIGHT_TO | DEPARTURE | ARRIVAL | SOME_OTHER_COL |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| Vueling | Barcelona | Paris | 01-NOV-22 | 01-NOV-22 | Some other stuff |
| RyanAir | Barcelona | Dublin | 01-NOV-22 | 01-NOV-22 | Some other stuff |
| KLM | Barcelona | Amsterdam | 01-NOV-22 | 01-NOV-22 | Some other stuff |
| Lufthansa | Barcelona | Frankfurt | 01-NOV-22 | 02-NOV-22 | Some other stuff |
As the question was "How do I display the date in the first column and time in the other 2 columns?" I would say that you are looking for this:

Select 
    FLIGHT_OPERATOR, FLIGHT_FROM, FLIGHT_TO, 
    DEPARTURE, To_Char(DEPARTURE, 'hh24:mi') "DEPARTURE_TIME", 
    ARRIVAL, To_Char(ARRIVAL, 'hh24:mi') "ARRIVAL_TIME", 
    SOME_OTHER_COL
From
    flights
Order By 
    DEPARTURE, FLIGHT_OPERATOR

Result would be:
| FLIGHT_OPERATOR | FLIGHT_FROM | FLIGHT_TO | DEPARTURE | DEPARTURE_TIME | ARRIVAL | ARRIVAL_TIME | SOME_OTHER_COL |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| Vueling | Barcelona | Paris | 01-NOV-22 | 10:45 | 01-NOV-22 | 12:30 | Some other stuff |
| RyanAir | Barcelona | Dublin | 01-NOV-22 | 11:10 | 01-NOV-22 | 13:00 | Some other stuff |
| KLM | Barcelona | Amsterdam | 01-NOV-22 | 20:10 | 01-NOV-22 | 23:00 | Some other stuff |
| Lufthansa | Barcelona | Frankfurt | 01-NOV-22 | 23:25 | 02-NOV-22 | 02:20 | Some other stuff |
So, the Departure and Arrival columns containe both the date and the time. You can select whatever part of that DateTime structure you want. In this answer I didn't take any part for Departure and Arrival but the columns as a whole. Their display format is defined by NLS_DATE_FORMAT (you can format it using NLS or some other way of displaying date/time). I just extracted time parts for departure and arrival dates in separate columns (like in the question) as there could be some night flights with different dates of departure and arrival.
Regards...

相关问题