Domanda

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).

È stato utile?

Soluzione

You can use the function next_day to get that:

SQL> select next_day(sysdate-7, 'MONDAY') FROM DUAL;

NEXT_DAY
---------
29-APR-13

Altri suggerimenti

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.

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;

You can truncate a date to Monday with:

select trunc(sysdate, 'IW') FROM DUAL;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top