在 Oracle SQL 中:如何将当前日期 + 时间插入表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32939206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Oracle SQL: How do you insert the current date + time into a table?
提问by Tikkaty
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);
回答by Gordon Linoff
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;
回答by brenners1302
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;