插入时间 to_date

Insert time with to_date

拜托,当我在 SQL/Oracle 中创建一个 table 时,例如:

CREATE TABLE Visite(
    id  NUMBER(5) PRIMARY KEY,
    dateVisite DATE,
    heureVisite DATE);

并插入一行:

Insert into visite values (1, TO_DATE('2018-12-14','yyyy-mm-dd'),TO_DATE('2:5:1','hh24:mi:ss'));

当我 select 结果时,我发现了类似的东西:

ID  DATEVISI HEUREVIS
---------- -------- ---------- ------------- --------
 1   14/12/18  01/02/18

虽然我预计:

ID  DATEVISI HEUREVIS
---------- -------- ---------- ------------- --------
 1   14/12/18  2:5:1

请有人帮我解释一下为什么?以及如何插入日期 hours:minute:second ?

正如@Kiran 所建议的,您可以将日期信息保存在 timestamp 类型列中,并在显示时以所需格式计算:

create table visite( id  number primary key, dateVisite timestamp );
/    
insert into visite values(1,to_timestamp('14/12/2018 02:05:01','dd/mm/yyyy hh24:mi:ss'));
/  
select id, to_char(dateVisite,'dd/mm/yyyy') dateVisite, to_char(dateVisite,'hh24:mi:ss') heureVisite from visite;
/
ID  DATEVISITE  HEUREVISITE
--  ----------  -----------
1   14/12/2018  02:05:01

您正在向两列插入 DATE(始终包含日期和时间值)。

TO_DATE('2018-12-14','yyyy-mm-dd') 给出 2018-12-14 00:00:00

当您输入没有日期值的 DATE 时,Oracle 将当月的第一天作为日期,因此 TO_DATE('2:5:1','hh24:mi:ss') 给出 2018-02-01 02:05:01

DATE Datatype

The default date values are determined as follows:

  • The year is the current year, as returned by SYSDATE.

  • The month is the current month, as returned by SYSDATE.

  • The day is 01 (the first day of the month).

  • The hour, minute, and second are all 0.

你看14/12/18 resp。 01/02/18 只是因为您当前的 NLS_DATE_FORMAT 会话设置似乎是 DD/MM/YY