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
Date type without time in Oracle
提问by Hieu Nguyen Trung
In Oracle 10g I got a problem when using DATE
type in my table. I just want my DATE
field store only DATE
without time automatically.
在 Oracle 10g 中,我DATE
在表中使用类型时遇到问题。我只想要我的DATE
现场存储,DATE
没有时间自动。
There's so much solution, I know it like using TO_CHAR
and TO_DATE
or TRUNC
but I faced that I'm using so much procedures to insert or update data and have no time to update all of them.
有很多解决方案,我知道它喜欢使用TO_CHAR
和TO_DATE
或,TRUNC
但我面临着我正在使用如此多的过程来插入或更新数据而没有时间更新所有这些。
How could I resolve this problem?
我该如何解决这个问题?
回答by Rob van Wijk
The best solution would be to:
最好的解决办法是:
remove all times from your DATE column (
update yourtable set yourdatecolumn = trunc(yourdatecolumn)
)ensure that all future dates contain no time part by placing a check constraint on the column by using
check (yourdatecolumn = trunc(yourdatecolumn))
adjust all your INSERT and UPDATE statements or -if you're lucky- adjust your API, to only insert TRUNCed dates.
从您的 DATE 列中删除所有时间 (
update yourtable set yourdatecolumn = trunc(yourdatecolumn)
)通过使用在列上放置检查约束来确保所有未来日期不包含时间部分
check (yourdatecolumn = trunc(yourdatecolumn))
调整您所有的 INSERT 和 UPDATE 语句,或者 - 如果幸运的话 - 调整您的 API,以仅插入 TRUNCed 日期。
The easiest solution would be to:
最简单的解决方案是:
(Optionally) remove all times from your DATE column.
Create a before row insert or update database trigger that sets
:new.yourdatecolumn := trunc(:new.yourdatecolumn);
(可选)从您的 DATE 列中删除所有时间。
创建一个设置前行插入或更新数据库触发器
: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...
就这样...