如何在oracle中获得2013年每个月的最后一个星期四?(How to get last thursday of every month in a year 2013 in oracle?)
如何在oracle中获得2013年每个月的最后一个星期四? 我需要将这个日期更新到我的表格中。
我需要一个类似的输出
Last Thursday in a year 2013
----------------------
31.01.2013
28.02.2013
28.03.2013
24.04.2013
30.05.2013
27.06.2013
25.07.2013
29.08.2013
26.09.2013
31.10.2013
28.11.2013
26.12.2013
多亏了需要。
How to get last Thursday of every month in a year 2013 in oracle? i need to update this date into my table.
i need a output like
Last Thursday in a year 2013
----------------------
31.01.2013
28.02.2013
28.03.2013
24.04.2013
30.05.2013
27.06.2013
25.07.2013
29.08.2013
26.09.2013
31.10.2013
28.11.2013
26.12.2013
Thanks to do the needful.
原文:https://stackoverflow.com/questions/17213977
更新时间:2019-09-12 05:42
最满意答案
这样做:
select next_day (last_day (add_months(date '2013-01-01', rownum-1))-7, 'THU') as thurs
from dual
connect by level <= 12;
THURS
---------
31-JAN-13
28-FEB-13
28-MAR-13
25-APR-13
30-MAY-13
27-JUN-13
25-JUL-13
29-AUG-13
26-SEP-13
31-OCT-13
28-NOV-13
26-DEC-13
12 rows selected.
说明:
1)以下select是一种生成一系列整数1..12的方法:
select rownum from dual connect by level <= 12;
2)按照2013年1月1日的规定返回2012年12个月中的第1个月,并添加0个月,1个月,......,11个月:
select add_months(date '2013-01-01', rownum-1)
from dual connect by level <= 12;
3) last_day函数返回给定日期的月份的最后一天,因此我们现在有2013-01-31,2013-02-28,...,2013-12-31。
4) next_day (date, 'THU')返回指定日期之后的下一个星期四。 要获得本月的最后一个星期四,我们将在该月的最后一天,返回7天,然后找到下一个星期四。
This will do it:
select next_day (last_day (add_months(date '2013-01-01', rownum-1))-7, 'THU') as thurs
from dual
connect by level <= 12;
THURS
---------
31-JAN-13
28-FEB-13
28-MAR-13
25-APR-13
30-MAY-13
27-JUN-13
25-JUL-13
29-AUG-13
26-SEP-13
31-OCT-13
28-NOV-13
26-DEC-13
12 rows selected.
Explanation:
1) The following select is a way to generate a series of integers 1..12:
select rownum from dual connect by level <= 12;
2) This returns the 1st of each of the 12 months of 2012 by taking 1st January 2013 and adding 0 months, 1 month, ..., 11 months:
select add_months(date '2013-01-01', rownum-1)
from dual connect by level <= 12;
3) The last_day function returns the last day of the month for the given date, so that we now have 2013-01-31, 2013-02-28, ..., 2013-12-31.
4) next_day (date, 'THU') returns the next Thursday after the specified date. To get the last Thursday of the month we take the last day of the month, go back 7 days, then find the next Thursday.
2013-06-21
相关问答
我认为在SQL Server中最接近Oracle的TO_CHAR函数的是CONVERT 。 但即使使用CONVERT我也没有办法只请求年份和月份。 相反,我们需要至少接受一天,除此之外还可能有一个时间戳。 CONVERT接受日期格式掩码参数作为其第三个参数。 112掩码仅以以下格式返回日期: yyyymmdd
所以对于你的问题中的日期,它会返回这个: 201707dd
在那里我没有定义dd因为我不知道给定的记录可能有哪一天。 现在我们可以通过输出这个输出的左边6个字符来接近你想要的,例如 WH
...
试试这段代码: private static Date getFirstThursday(Date now) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.set(Calendar.WEEK_OF_MONTH, 1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
return cal.getTime();
}
Try this code:
...
尝试 where to_date(year || month, 'YYYYMM')
between add_months(trunc(sysdate, 'MM'), -8) and trunc(sysdate)
Try where to_date(year || month, 'YYYYMM')
between add_months(trunc(sysdate, 'MM'), -8) and trunc(sysdate)
阅读PHP Bug#53539 。 解决方法是使用: strtotime("first thursday of december 2011") 请注意,PHP 5.3中修复了许多DateTime错误。 如果你运行<5.3,你应该真的升级。 Read PHP Bug #53539. The fix is to use of: strtotime("first thursday of december 2011") Note that many DateTime bugs were fixed in
...
这样做: select next_day (last_day (add_months(date '2013-01-01', rownum-1))-7, 'THU') as thurs
from dual
connect by level <= 12;
THURS
---------
31-JAN-13
28-FEB-13
28-MAR-13
25-APR-13
30-MAY-13
27-JUN-13
25-JUL-13
29-AUG-13
26-SEP-13
31-OCT-13
28-NOV-1
...
默认情况下,Java Calendar是宽松的。 如果将其设置为1月32日,则会将其转换为2月1日。此外,“周”的定义是特定于区域设置的,有点令人困惑。 在上面链接的JavaDocs中详细解释了这一点。 在您的特定情况下(或至少在我的区域设置中),一周被定义为从星期日开始,“六月的第一周”被定义为包括6月1日的星期日到星期六的时段。2013年6月1日是星期六,2013年第22周的最后一天。2013年6月2日星期日,是6月第2周的第一天,而不是第1周的第二天。 由于6月第1周没有星期四,因此宽松日历
...
在过去的两个小时里我一直在努力 - 只要我在这里发布,我就得到了解决方案。 我只需要将DateInterval字符串更改为“下个月的第一个星期四”。 $start = new DateTime('first thursday of June 2012');
$end = new DateTime('2012-12-31');
$interval = DateInterval::createFromDateString('first thursday of next month');
$per
...
这将与本月第二个星期四的日子相呼应: <?php
echo date('d', strtotime('second thursday of this month'));
// Will echo 10, which is correct for april 2014
echo date('d', strtotime('second thursday of next month'));
// Will echo 08, which is correct for may 2014
echo dat
...