简体   繁体   English

如果我通过Oracle SQL知道年份和星期编号,如何获取开始日期?

[英]How to get begin date if I know year and week number via Oracle SQL?

How can I get the begin date via Oracle SQL if I provide year and week number? 如果提供年和周号,如何通过Oracle SQL获取开始日期? For example, giving year 2014 and week number 1 , get date 2014-01-05 . 例如,给定2014和第1周,则获取日期2014-01-05

Does not go that simply, but the next_day() function is of help here: 不能简单地做到这一点,但是next_day()函数在这里有帮助:

select
    next_day(to_date(&year||'01-01','yyyy-mm-dd'), 'SUN')
        + 7 * (&week-1)
from dual;

(Thanks to Alex Poole for pointing out ...) The second parameter to the next_day() function is NLS-specific, as is the concept of the "start of week". (感谢Alex Poole指出...)next_day()函数的第二个参数是NLS特定的,“周开始”的概念也是如此。 So you'll have to play with NLS settings for your session and think about potential portability of your solution to other countries. 因此,您必须在会话中使用NLS设置,并考虑解决方案向其他国家/地区的潜在可移植性。

You can modify the year as per your need, the query will list all the dates which are first Sunday of the week for that Year 您可以根据需要修改年份,查询将列出该Year第一个Sunday的所有日期

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM