问题
I'm using an Oracle 9i database and want to obtain, within a function, the timestamp representing the start of the week, i.e. The most recent monday, at 00:00:00.
I am aware that the timestamp representing the start of the current day is TO_TIMESTAMP(SYSDATE)
.
回答1:
You can use the function next_day
to get that:
SQL> select next_day(sysdate-7, 'MONDAY') FROM DUAL;
NEXT_DAY
---------
29-APR-13
回答2:
Getting the start of the week should work with trunc
(see docs).
So,
select to_timestamp(trunc(sysdate, 'D')) from dual
should work.
However, depending on your NLS settings, the first day of the week for oracle may well be Sunday.
回答3:
this appears to return Monday before the day of week in question at midnight. to prove out just play around with sysdate and subtract days...
select case when to_Char(sysdate,'d') = 1 then
trunc(sysdate-6)
else
trunc(sysdate - (to_Char(sysdate,'d')-2))
END
from dual;
回答4:
You can truncate a date to Monday with:
select trunc(sysdate, 'IW') FROM DUAL;
来源:https://stackoverflow.com/questions/16289302/in-oracle-sql-how-would-you-obtain-the-timestamp-representing-the-start-of-the