简体   繁体   English

如何在 Oracle SQL 中分隔日期和时间

[英]How to seperate Date and Time in Oracle SQL

I have a table called transaction that contains a TxDateTime which is a DATE data type.我有一个名为 transaction 的表,其中包含一个 DATE 数据类型的 TxDateTime。 When I run a query I would like to separate the date and time into their own columns;当我运行查询时,我想将日期和时间分成各自的列; TxDate and TxTime.发送日期和发送时间。 How can I achieve this??我怎样才能做到这一点?

Here is how I created the table and inserted the date/time:这是我创建表格并插入日期/时间的方式:

Table creation表创建

CREATE TABLE TRANSACTION(TxNbr INTEGER PRIMARY KEY,
                         TxCode CHAR(1) NOT NULL,
                         AccountNbr INTEGER NOT NULL,
                         Amount DECIMAL(13,2) NOT NULL,
                         TxDateTime DATE, 
                         RefNbr VARCHAR(3),
                         FOREIGN KEY(AccountNbr) REFERENCES ACCOUNT (AccountNbr) ON DELETE SET NULL,
                         FOREIGN KEY(TxCode) REFERENCES TX_TYPE (TxCode) ON DELETE SET NULL
                         );

Insert into table插入表格

 INSERT INTO TRANSACTION VALUES(TxNbr_Seq.nextval, 'X', 1000001, 123.45, TO_DATE('2019/05/01 12:00', 'yyyy/mm/dd hh24:mi'), '101');

Select Select

SELECT Transaction.TxDateTime,
FROM TRANSACTION

The output only gives the date: 19-05-01, but I would like the date and time in separate columns when I run a query. output 仅给出日期:19-05-01,但我希望在运行查询时将日期和时间放在单独的列中。

Oracle doesn't have a time data type. Oracle 没有time数据类型。 You can convert to a string:您可以转换为字符串:

select to_char(TxDateTime, 'YYYY-MM-DD') as date, to_char(TxDateTime, 'HH24:MI:SS')
from transaction t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM