Oracle 中没有时间的日期类型

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

Date type without time in Oracle

oracledatetimedateoracle10g

提问by Hieu Nguyen Trung

In Oracle 10g I got a problem when using DATEtype in my table. I just want my DATEfield store only DATEwithout time automatically.

在 Oracle 10g 中,我DATE在表中使用类型时遇到问题。我只想要我的DATE现场存储,DATE没有时间自动。

There's so much solution, I know it like using TO_CHARand TO_DATEor TRUNCbut I faced that I'm using so much procedures to insert or update data and have no time to update all of them.

有很多解决方案,我知道它喜欢使用TO_CHARTO_DATE或,TRUNC但我面临着我正在使用如此多的过程来插入或更新数据而没有时间更新所有这些。

How could I resolve this problem?

我该如何解决这个问题?

回答by Rob van Wijk

The best solution would be to:

最好的解决办法是:

  1. remove all times from your DATE column (update yourtable set yourdatecolumn = trunc(yourdatecolumn))

  2. ensure that all future dates contain no time part by placing a check constraint on the column by using check (yourdatecolumn = trunc(yourdatecolumn))

  3. adjust all your INSERT and UPDATE statements or -if you're lucky- adjust your API, to only insert TRUNCed dates.

  1. 从您的 DATE 列中删除所有时间 ( update yourtable set yourdatecolumn = trunc(yourdatecolumn))

  2. 通过使用在列上放置检查约束来确保所有未来日期不包含时间部分 check (yourdatecolumn = trunc(yourdatecolumn))

  3. 调整您所有的 INSERT 和 UPDATE 语句,或者 - 如果幸运的话 - 调整您的 API,以仅插入 TRUNCed 日期。

The easiest solution would be to:

最简单的解决方案是:

  1. (Optionally) remove all times from your DATE column.

  2. Create a before row insert or update database trigger that sets :new.yourdatecolumn := trunc(:new.yourdatecolumn);

  1. (可选)从您的 DATE 列中删除所有时间。

  2. 创建一个设置前行插入或更新数据库触发器 :new.yourdatecolumn := trunc(:new.yourdatecolumn);

回答by zhuman

i use like this

我这样用

insert : Insert into table (name_date) values(to_date(current_date,'dd/mm/yyyy'));update : update table set name_date=to_date(current_date,'dd/mm/yyyy') where id=1;

插入:Insert into table (name_date) values(to_date(current_date,'dd/mm/yyyy'));更新:update table set name_date=to_date(current_date,'dd/mm/yyyy') where id=1;

that's all...

就这样...