在Oracle SQL中:如何将当前日期+时间插入表中?

时间:2022-10-03 21:53:36

I've written below code, but it only seems to insert the current date and not the current time. Anyone knows how to do that?

我写了下面的代码,但它似乎只插入当前日期而不是当前时间。谁知道怎么做?

insert into errortable
(dateupdated,table1id)
values
(TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),1083);

2 个解决方案

#1


10  

It only seems to because that is what it is printing out. But actually, you shouldn't write the logic this way. This is equivalent:

它似乎只是因为它正在打印出来。但实际上,你不应该这样写逻辑。这相当于:

insert into errortable (dateupdated, table1id)
    values (sysdate, 1083);

It seems silly to convert the system date to a string just to convert it back to a date.

将系统日期转换为字符串只是为了将其转换回日期似乎很愚蠢。

If you want to see the full date, then you can do:

如果你想看到完整的日期,那么你可以这样做:

select TO_CHAR(dateupdated, 'YYYY-MM-DD HH24:MI:SS'), table1id
from errortable;

#2


-4  

You may try with below query :

您可以尝试使用以下查询:

INSERT INTO errortable (dateupdated,table1id)
VALUES (to_date(to_char(sysdate,'dd/mon/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss' ),1083 );

To view the result of it:

要查看它的结果:

SELECT to_char(hire_dateupdated, 'dd/mm/yyyy hh24:mi:ss') 
FROM errortable 
    WHERE table1id = 1083;

#1


10  

It only seems to because that is what it is printing out. But actually, you shouldn't write the logic this way. This is equivalent:

它似乎只是因为它正在打印出来。但实际上,你不应该这样写逻辑。这相当于:

insert into errortable (dateupdated, table1id)
    values (sysdate, 1083);

It seems silly to convert the system date to a string just to convert it back to a date.

将系统日期转换为字符串只是为了将其转换回日期似乎很愚蠢。

If you want to see the full date, then you can do:

如果你想看到完整的日期,那么你可以这样做:

select TO_CHAR(dateupdated, 'YYYY-MM-DD HH24:MI:SS'), table1id
from errortable;

#2


-4  

You may try with below query :

您可以尝试使用以下查询:

INSERT INTO errortable (dateupdated,table1id)
VALUES (to_date(to_char(sysdate,'dd/mon/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss' ),1083 );

To view the result of it:

要查看它的结果:

SELECT to_char(hire_dateupdated, 'dd/mm/yyyy hh24:mi:ss') 
FROM errortable 
    WHERE table1id = 1083;