oracle 在oracle中插入系统日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15779321/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 04:56:35  来源:igfitidea点击:

Inserting system date in oracle

oracle

提问by user2238881

How to insert system date in dd/mm/yyyy to table using oracle 10g?

如何使用oracle 10g将dd/mm/yyyy中的系统日期插入到表格中?

While using the following query, it inserts system date as 03/04/0013. I need 03/04/2013. Can you help me to solve this problem?

使用以下查询时,它将系统日期插入为 03/04/0013。我需要 03/04/2013。你能帮我解决这个问题吗?

insert into GtTable 
values('111','Name',300,'Tour',to_date('02/04/2012','DD/MM/YYYY'),to_date(sysdate,'DD/MM/YYYY'));

But when inserting '02/04/2012' directly, it accepts as same as '02/04/2012'.

但是当直接插入'02/04/2012'时,它接受与'02/04/2012'相同。

回答by A.B.Cade

You shouldn't activate TO_DATEon a date

你不应该TO_DATE在约会时激活

sysdateis already a date, when you run TO_DATEwith it as the first parameter, you make Oracle implicitly convert it to a string according to NLS_DATE_FORMATwhich in your case probably contains YYand not YYYY.

sysdate已经是一个日期,当你将TO_DATE它作为第一个参数运行时,你让 Oracle 隐式地将它转换为一个字符串,根据NLS_DATE_FORMAT你的情况可能包含YY而不是YYYY.

A date in oracle is a number representing a date and time, it doesn't have a "format", if you want to insert sysdate without the time value you need to truncate it like this:

oracle 中的日期是表示日期和时间的数字,它没有“格式”,如果要插入没有时间值的 sysdate,则需要像这样截断它:

insert into GtTable 
values('111','Name',300,'Tour',to_date('02/04/2012','DD/MM/YYYY'),trunc(sysdate))

回答by warantesbr

If the last field on your insert is a date type field, you should not need any conversion on SYSDATE, so, the following should be OK:

如果插入的最后一个字段是日期类型字段,则不需要对 SYSDATE 进行任何转换,因此,以下内容应该没问题:

insert into GtTable 
values('111', 'Name', 300, 'Tour', to_date('02/04/2012','DD/MM/YYYY'), sysdate);

But if it is a varchar field, the following should work:

但如果它是一个 varchar 字段,以下应该工作:

insert into GtTable 
values('111', 'Name', 300, 'Tour', to_date('02/04/2012','DD/MM/YYYY'), to_char(sysdate, 'dd/mm/yyyy'));

回答by Gentlezerg

I think the format of displaying is due to the environment parameter。 TO_CHAR(SYSDATE,'DD/MM/YYYY') CAN LET YOU inserts system date as DD/MM/YYYY ,but, it is not date type anymore.

我认为显示的格式是由于环境参数。 TO_CHAR(SYSDATE,'DD/MM/YYYY') CAN LET YOU 将系统日期插入为 DD/MM/YYYY ,但是,它不再是日期类型了。

回答by Jorge Santos Neill

The solution that worked for me is the following

对我有用的解决方案如下

insert into method (id,name,created_by,updated_by, created_at, updated_at) values (1,'GET',0,0,TO_CHAR(SYSDATE,'DD/MM/YYYY hh:mm:ss'),null);