我们有一列保存日期(MM / DD / YYYY),该日期是某人的帐户最近一次由自动服务更新的日期。这个日期过了两天后,我们就会提供另一项自动服务,该服务会自动设置帐户的保留状态。
基本上,我需要返回将保留实际置于帐户中的日期。以下是我的代码,用于返回所有已放置此用户的用户。 e.lastupdate是需要添加两天的字段
select p.id||' '||p.lastname||' '||p.firstname||' '|| e.lastupdate
from table p, othertable e
where p.id = e.id
and hold = 8
and id in (
select id from othertable
where buildinginfo is null
)
order by id;
预期结果应在“ e.lastupdate”字段之后两天
答案 0 :(得分:0)
我不确定您要寻找的答案是否仅仅是关于如何处理日期的问题?如果是这样,请参阅我的答案,其中显示了如何对日期/时间进行计算。
下面仅在查询中添加了一个名为lastupdate_plus_2_days的新列
SELECT p.id||' '||p.lastname||' '||p.firstname||' '|| e.lastupdate + 2
, e.lastupdate + 2 AS lastupdate_plus_2_days
FROM some_TABLE p, othertable e
WHERE p.id = e.id
AND hold = 8
AND id IN
(
SELECT id
FROM othertable
WHERE buildinginfo IS NULL
)
ORDER BY id;
处理日期的示例
/* Working with SYSDATE and to_char formats */
SELECT
sysdate -365 Last_Year,
to_char(to_date('04-DEC-2013', 'DD-MON-YY') - 365, 'DD-MON-YYYY') AS Last_Year2,
add_months(sysdate, -1) Last_Month,
sysdate -7 Last_Week,
sysdate -1 Yesterday,
sysdate Today,
to_char(sysdate, 'DD-MON-YY HH:MI:SS') NOW,
to_char(sysdate + 1/(24*60*60), 'DD-MON-YY HH:MI:SS') "OneSecFrom_NOW",
to_char(sysdate + 2/(24*60*60), 'DD-MON-YY HH:MI:SS') "TwoSecFrom_NOW",
to_char(sysdate + 1/(24*60), 'DD-MON-YY HH:MI:SS') "OneMinFrom_NOW",
to_char(sysdate + 7/(24*60), 'DD-MON-YY HH:MI:SS') "SevMinFrom_NOW",
to_char(sysdate + 1/24, 'DD-MON-YY HH:MI:SS') "OneHrFrom_NOW",
sysdate +1 Tomorrow,
sysdate +7 Next_Week,
last_day(sysdate) as last_day_of_month,
add_months(sysdate, 1) Next_Month,
last_day(add_months(sysdate, 1)) as last_day_of_next_month,
sysdate +365 Next_Year,
/* Below are examples of how you could disect a number of days and spit out how many years, months, days are within those days using the mod function. */
trunc((to_date('02-FEB-14') - to_date('01-JAN-13'))/365) "~yr", trunc(mod((to_date('02-FEB-14') - to_date('01-JAN-13')),365)/31) "~mon", mod(mod((to_date('02-FEB-14') - to_date('01-JAN-13')),365),31) "~days"
FROM
dual;
这个答案是您要找的吗?除非在某处执行“ UPDATE”的代码,否则从“ SELECT”查询返回的原始日期将永远不会更改要从中提取数据的表中的原始日期值。