用于更新 PST 系统日期/时间的列
我在 IG 中有两栏(状态和更新日期)。每次状态列发生变化时,我想记录它的更新时间(以 PST 格式)。这是我的做法,但它没有按预期工作。
我在更新日期列上创建了一个动态操作,以在更改时设置该值。但是,我更新的专栏显示了一些不正确的时间/日期。
SELECT
TO_CHAR(NEW_TIME( TO_DATE( CAST(SYSDATE as DATE), 'MM-DD-YYYY HH24:MI:SS' ), 'GMT', 'PST' ), 'MM-DD-YYYY HH24:MI:SS') TIME_IN_PST
FROM
TRANSITION_TASKS_NEW;
I have two columns (Status & Updated Date) in IG. Everytime the status column is change with something, I want to log the time it is updated (in PST). Here is how I'm doing it but its not working as expected.
I have created a dynamic action on my updated date column to set the value when it is changed. However, my updated column shows some incorrect time/date.
SELECT
TO_CHAR(NEW_TIME( TO_DATE( CAST(SYSDATE as DATE), 'MM-DD-YYYY HH24:MI:SS' ), 'GMT', 'PST' ), 'MM-DD-YYYY HH24:MI:SS') TIME_IN_PST
FROM
TRANSITION_TASKS_NEW;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SYSDATE
已经是一个日期,因此您不需要对其使用CAST
。同样,由于它已经是一个日期,因此您不需要在其上使用TO_DATE
,这可能会导致错误。因此,您的代码可以是:
但是,您可以使用
SYSTIMESTAMP
(返回TIMESTAMP
数据类型)使其变得更加简单,然后您不需要使用>NEW_TIME
函数:两者输出:
db<>fiddle 此处
至于导致代码不正确的原因:
TO_DATE
函数采用字符串作为第一个参数。当您这样做时:那么 Oracle 会将其隐式转换为:
如果
NLS_DATE_FORMAT
会话参数不是MM-DD-YYYY HH24:MI:SS
(且 默认 NLS 日期格式 与此不匹配),那么您将收到错误,或者更糟糕的是,意外且可能无效 结果。例如:
输出:
其中有错误的时间成分和错误的世纪!
要解决此问题,请勿对已经是
DATE
的值使用TO_DATE
。db<>fiddle 此处
SYSDATE
is already a date so you do not need to useCAST
on it. Similarly, since it is already a date you do not need to useTO_DATE
on it and this is likely to cause errors.So your code can just be:
However, you can make it even simpler using
SYSTIMESTAMP
(which returns aTIMESTAMP
data type) and then you do not need to use theNEW_TIME
function:Which both output:
db<>fiddle here
As for what is causing your code to be incorrect:
The
TO_DATE
function takes a string as the first argument. When you do:Then Oracle will implicitly convert it to:
If the
NLS_DATE_FORMAT
session parameter is notMM-DD-YYYY HH24:MI:SS
(and the default NLS date format never matches that) then you will get either an error or, worse, an unexpected and probably invalid result.For example:
Outputs:
Which has the wrong time component AND the wrong century!
To solve this, do not use
TO_DATE
on values that are alreadyDATE
s.db<>fiddle here