问题描述
在Oracle 10g中,当我在表中使用 DATE
类型时,我遇到了一个问题。我只是希望我的 DATE
字段存储只有 DATE
没有时间自动。
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.
有很多解决方案,我知道它就像使用 TO_CHAR
和 TO_DATE
或 TRUNC
,但是我面对的是我使用这么多的程序来插入或更新数据,没有时间更新所有这些。
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.
我可以解决这个问题吗?
How could I resolve this problem?
推荐答案
最好的解决方法是:
-
从DATE列中删除所有时间(
更新yourtable set yourdatecolumn = trunc(yourdatecolumn)
)
通过使用 check(yourdatecolumn = trunc(yourdatecolumn))$ c)在列上放置检查约束,确保所有未来的日期都不包含时间部分$ c>
调整所有INSERT和UPDATE语句,或者 - 如果您幸运 - 调整您的API,只能插入TRUNCed日期。
adjust all your INSERT and UPDATE statements or -if you're lucky- adjust your API, to only insert TRUNCed dates.
最简单的解决方案将是:
The easiest solution would be to:
-
(可选)从DATE列中删除所有时间。
(Optionally) remove all times from your DATE column.
创建前一行插入或更新数据库触发器,设置:new.yourdatecolumn:= trunc(:new.yourdatecolumn);
Create a before row insert or update database trigger that sets :new.yourdatecolumn := trunc(:new.yourdatecolumn);
这篇关于Oracle中没有时间的日期类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!