我正在学习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为时间。
3条答案
按热度按时间uqcuzwp81#
在Oracle中,
DATE
是一种二进制数据类型,由7个字节组成,代表世纪、世纪年份、月、日、小时、分钟和秒。它总是具有所有这些组件,并且从不以任何特定(人类可读)格式存储。因此,如果您有一个
DATE
,它将始终既是日期又是时间。如果只想存储日期的日期部分,则需要使用整个日期,但可以添加一个约束以确保时间始终为午夜。
如果要存储不带日期的时间,则可以:
1.使用
DATE
数据类型,只设置时间组件(忽略日期组件的默认值);或1.使用
INTERVAL DAY TO SECOND
数据类型。例如,您的数据表可以是:
或者,您可以将表格简化为:
而不用担心有不同的日期和时间。
nbysray52#
一种选择是更改会话的NLS,并同时影响 * 所有 *
DATE
数据类型列。样表及部分数据:
以下是my数据库返回的结果:
现在,将格式修改为其他格式:
结果:
这是一个SQL*Plus示例,但在SQL Developer中同样适用。
或者,打开SQL Developer的首选项(在“工具”菜单中),搜索“NLS”并输入所需的日期格式。
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:
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:
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...