日期格式图片在转换整个输入字符串之前结束的解决办法(四)

2014-11-24 17:04:40 · 作者: · 浏览: 6
select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') last_day
from (select substr('2004-32', 1, 4) yy, to_number(substr('2004-32', 6)) ww
from dual)
select trunc(to_date(substr('2003-01',1,5)||to_char((to_number(substr('2003-01',6)))*7),'yyyy-ddd'),'d')-6 first_day from dual
select min(v_date) from
(select (to_date('200201','yyyymm') + rownum) v_date
from all_tables
where rownum < 370)
where to_char(v_date,'yyyy-iw') = '2002-49'
2.查询某周的最后一天
select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') - 6 first_day
from (select substr('2004-33', 1, 4) yy, to_number(substr('2004-33', 6)) ww
from dual)
select trunc(to_date(substr('2003-01',1,5)||to_char((to_number(substr('2003-01',6)))*7),'yyyy-ddd'),'d') last_day from dual
select max(v_date) from
(select (to_date('200408','yyyymm') + rownum) v_date
from all_tables
where rownum < 370)
where to_char(v_date,'yyyy-iw') = '2004-33'
3.查询某周的日期
select min_date, to_char(min_date,'day') day from
(select to_date(substr('2004-33',1,4)||'001'+rownum-1,'yyyyddd') min_date
from all_tables
where rownum <= decode(mod(to_number(substr('2004-33',1,4)),4),0,366,365)
union
select to_date(substr('2004-33',1,4)-1||
decode(mod(to_number(substr('2004-33',1,4))-1,4),0,359,358)+rownum,'yyyyddd') min_date
from all_tables
where rownum <= 7
union
select to_date(substr('2004-33',1,4)+1||'001'+rownum-1,'yyyyddd') min_date
from all_tables
where rownum <= 7
)
where to_char(min_date,'yyyy-iw') ='2004-33'
oracle中时间运算
论坛中常常看到有对oracle中时间运算提问的问题,今天有时间,看了看以前各位兄弟的贴子,整理了一下,并作了个示例,希望会对大家有帮助。
首先感谢ern、eric.li及各版主还有热心的兄弟们
内容如下:
1、oracle支持对日期进行运算
2、日期运算时是以天为单位进行的
3、当需要以分秒等更小的单位算值时,按时间进制进行转换即可
4、进行时间进制转换时注意加括号(见示例中红色括号),否则会出问题
SQL> alter session set nls_date_format='yyyy-mm-dd hh:mi:ss';
会话已更改。
SQL> set serverout on
SQL> declare
2 Dateva lue date;
3 begin
4 select sysdate into Dateva lue from dual;
5 dbms_output.put_line('源时间:'||to_char(Dateva lue));
6 dbms_output.put_line('源时间减1天:'||to_char(Dateva lue-1));
7 dbms_output.put_line('源时间减1天1小时:'||to_char(Dateva lue-1-1/24));
8 dbms_output.put_line('源时间减1天1小时1分:'||to_char(Dateva lue-1-1/24-1/(24*60)));
9 dbms_output.put_line('源时间减1天1小时1分1秒:'||to_char(Dateva lue-1-1/24-1/(24*60)-1/(24*60*6
0)));
10 end;
11 /
源时间:2003-12-29 11:53:41
源时间减1天:2003-12-28 11:53:41
源时间减1天1小时:2003-12-28 10:53:41
源时间减1天1小时1分:2003-12-28 10:52:41
源时间减1天1小时1分1秒:2003-12-28 10:52:40
PL/SQL 过程已成功完成。