给定年,月,日和月的周数,我如何找到日期?
问题描述:
Year : 2011
Month: Nov
Day: Sun
WeekNumber(Monthwise): 4
欲望输出:
Date
--------
2011-11-20
我怎么能在一个单独的SQL语句这样做呢?
感谢
答
我可能会看使用NEXT_DAY
函数返回给定的日期之后发生提供平日的第一天。一旦你有了,你可以添加所需的周数。
这方面的一个例子是:
with test_data as (
select
'2011' as the_year,
'Nov' as the_month,
'Sun' as the_day,
4 as the_week
from dual
)
select
the_year, the_month, the_day, the_week,
next_day(to_date(the_year||the_month, 'YYYYMON') - 1, the_day) +
7* (the_week -1) as the_date
from test_data
答
尝试:
SELECT A.B + B.D YourDate
FROM (SELECT TO_DATE (Your4DigitYear || LPAD (YourMonth, 2, '0') || LPAD (DECODE (YourWeekOfMonth, 1, '1', (YourWeekOfMonth - 1) * 7), 2, '0'), 'YYYYMMDD') B FROM DUAL) A, (SELECT LEVEL - 1 D FROM DUAL CONNECT BY LEVEL < 15) B WHERE
TO_CHAR (A.B + B.D, 'D') = YourDayOfWeek AND
TO_CHAR (A.B + B.D, 'W') = YourWeekOfMonth;
答
这可能工作。
SELECT NEXT_DAY(TO_DATE(TO_CHAR((4-1)*7) || '-' || 'NOV' || '-' || '2011','dd-mon-yyyy') ,'Sun')
FROM DUAL
见:http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions093.htm
+0
这是给错误ORA-01843:不是有效的月份 – user1025901
+0
工作对我来说,错误可能是因为我没有指定日期格式,已经更新。 –
+1,请参阅:http://www.techonthenet.com/oracle/functions/next_day.php – Johan