Oracle计算自然周

 

http://blog.csdn.net/wh62592855/article/details/4739492

 

create or replace function Getweekbydate(sdate in date) return number is
  Result number;
begin
  select to_char(decode(sign((to_date(to_char(sdate, 'yyyymmdd'),
                                      'yyyymmdd') +
                             to_number(decode(to_char(trunc(to_date(to_char(sdate,
                                                                             'yyyymmdd'),
                                                                     'yyyymmdd'),
                                                             'yyyy'),
                                                       'd'),
                                               '1',
                                               '8',
                                               to_char(trunc(to_date(to_char(sdate,
                                                                             'yyyymmdd'),
                                                                     'yyyymmdd'),
                                                             'yyyy'),
                                                       'd'))) - 2) -
                             last_day(to_date(to_char(sdate, 'yyyymmdd'),
                                              'yyyymmdd'))),
                        1,
                        last_day(to_date(to_char(sdate, 'yyyymmdd'),
                                         'yyyymmdd')),
                        (to_date(to_char(sdate, 'yyyymmdd'), 'yyyymmdd') +
                        to_number(decode(to_char(trunc(to_date(to_char(sdate,
                                                                        'yyyymmdd'),
                                                                'yyyymmdd'),
                                                        'yyyy'),
                                                  'd'),
                                          '1',
                                          '8',
                                          to_char(trunc(to_date(to_char(sdate,
                                                                        'yyyymmdd'),
                                                                'yyyymmdd'),
                                                        'yyyy'),
                                                  'd'))) - 2)),
                 'ww')
    into result
    from dual;

  return Result;
end Getweekbydate;

 

 

 

posted @ 2013-01-14 11:53  吃西瓜最快  阅读(869)  评论(0编辑  收藏  举报