如果我提供年份和周数,如何通过Oracle SQL获取开始日期?
例如,提供年份2014
和周数1
,请获取日期2014-01-05
。
答案 0 :(得分:1)
不是那么简单,但next_day()函数在这里有帮助:
select
next_day(to_date(&year||'01-01','yyyy-mm-dd'), 'SUN')
+ 7 * (&week-1)
from dual;
(感谢Alex Poole指出......) next_day()函数的第二个参数是NLS特定的,"周开始的概念"。因此,您必须为会话使用NLS设置,并考虑将解决方案的潜在可移植性提升到其他国家/地区。
答案 1 :(得分:0)
您可以根据需要修改年份,查询会列出Sunday
Year
的所有日期
SQL> SELECT YEAR,
2 week,
3 next_day(to_date(YEAR
4 ||'01-01','yyyy-mm-dd'), 'SUN') + (week-1)* 7
5 FROM
6 (SELECT '2014' YEAR, ROWNUM week FROM all_objects WHERE ROWNUM <= 53
7 )
8 /
YEAR WEEK NEXT_DAY(
---- ---------- ---------
2014 1 05-JAN-14
2014 2 12-JAN-14
2014 3 19-JAN-14
2014 4 26-JAN-14
2014 5 02-FEB-14
2014 6 09-FEB-14
2014 7 16-FEB-14
2014 8 23-FEB-14
2014 9 02-MAR-14
2014 10 09-MAR-14
2014 11 16-MAR-14
2014 12 23-MAR-14
2014 13 30-MAR-14
2014 14 06-APR-14
2014 15 13-APR-14
2014 16 20-APR-14
2014 17 27-APR-14
2014 18 04-MAY-14
2014 19 11-MAY-14
2014 20 18-MAY-14
2014 21 25-MAY-14
2014 22 01-JUN-14
2014 23 08-JUN-14
2014 24 15-JUN-14
2014 25 22-JUN-14
2014 26 29-JUN-14
2014 27 06-JUL-14
2014 28 13-JUL-14
2014 29 20-JUL-14
2014 30 27-JUL-14
2014 31 03-AUG-14
2014 32 10-AUG-14
2014 33 17-AUG-14
2014 34 24-AUG-14
2014 35 31-AUG-14
2014 36 07-SEP-14
2014 37 14-SEP-14
2014 38 21-SEP-14
2014 39 28-SEP-14
2014 40 05-OCT-14
2014 41 12-OCT-14
2014 42 19-OCT-14
2014 43 26-OCT-14
2014 44 02-NOV-14
2014 45 09-NOV-14
2014 46 16-NOV-14
2014 47 23-NOV-14
2014 48 30-NOV-14
2014 49 07-DEC-14
2014 50 14-DEC-14
2014 51 21-DEC-14
2014 52 28-DEC-14
2014 53 04-JAN-15
53 rows selected.
SQL>