In oracle SQL, how would you obtain the timestamp representing the start of the week?

跟風遠走 提交于 2019-12-13 06:27:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!