我正在寻找一种方法来获得特定日期去年的相同日期。
我有一张有日期的表格,并希望在同一天的第二列,但去年同一天。
实施例: 日期:2009年6月1日星期一 去年日期:2008年6月2日星期一
所以它必须是同一周的同一天,但提前一年。
有没有办法做到这一点?
感谢您的帮助?
答案 0 :(得分:0)
尝试:
select add_months(sysdate, -12) + (to_char(sysdate, 'D') - to_char(add_months(sysdate, -12), 'D'))
from dual
输出:
6/10/2014 12:07:01 PM
这也是今天的星期二。
答案 1 :(得分:0)
你可以使用这个:
SELECT
NEXT_DAY(ADD_MONTHS(DATE '2009-06-01', -12)-1, TO_CHAR(DATE '2009-06-01', 'fmDAY')) AS Last_year_day
FROM dual;
结果:
2008-06-02
当然,它总是会返回下一个日,即6天后的最坏情况。
答案 2 :(得分:0)
假设"周"你的意思是ISO-8601定义的周,你可以使用一个函数将周数转换为日期。
CREATE FUNCTION IsoWeekDate(iso_year IN INTEGER, iso_week IN INTEGER) RETURN DATE AS
BEGIN
RETURN TRUNC(TO_DATE(iso_year || '0104', 'YYYYMMDD'), 'IW') + 7 * (iso_week - 1);
END IsoWeekDate;
然后你可以这样做:
SELECT
NEXT_DAY(IsoWeekDate(TO_CHAR(DATE '2009-06-01', 'IYYY')-1, TO_CHAR(DATE '2009-06-01', 'IW'))-1, TO_CHAR(DATE '2009-06-01', 'fmDAY'))
FROM YOUR_TABLE
当然,您可以在SQL中编写所有内容,我将其拆分以获得更好的可见性。
请注意,对于12月29日到1月3日之间的日期,当您使用此方法时,您可能会得到意想不到的结果,因为一年可能有52或53周,这些日期可能属于之前的,相应的。明年。
答案 3 :(得分:0)
Oracle 11g R2架构设置:
CREATE FUNCTION TO_ISO_WEEK_DATE(
week NUMBER,
year NUMBER
) RETURN DATE DETERMINISTIC
IS
BEGIN
RETURN NEXT_DAY(
TO_DATE( TO_CHAR( year, '0000' ) || '0104', 'YYYYMMDD' )
- INTERVAL '7' DAY, 'MONDAY'
)
+ ( week - 1 ) * 7;
END TO_ISO_WEEK_DATE;
/
CREATE FUNCTION SAME_DAY_AND_WEEK_OF_YEAR(
dt DATE,
year NUMBER
) RETURN DATE DETERMINISTIC
IS
p_week NUMBER(2,0) := TO_NUMBER( TO_CHAR( dt, 'IW' ) );
p_day VARCHAR2(9) := TO_CHAR( dt, 'DAY' );
BEGIN
RETURN NEXT_DAY( TO_ISO_WEEK_DATE( p_week, year ) - 1, p_day );
END SAME_DAY_AND_WEEK_OF_YEAR;
/
查询1 :
SELECT TO_CHAR( SYSDATE + LEVEL - 1, 'YYYY-MM-DD (IW-D)' ) AS "Date 2015",
TO_CHAR( SAME_DAY_AND_WEEK_OF_YEAR( SYSDATE + LEVEL - 1, 2014 ), 'YYYY-MM-DD (IW-D)' ) AS "Date 2014",
TO_CHAR( SAME_DAY_AND_WEEK_OF_YEAR( SYSDATE + LEVEL - 1, 2013 ), 'YYYY-MM-DD (IW-D)' ) AS "Date 2013",
TO_CHAR( SAME_DAY_AND_WEEK_OF_YEAR( SYSDATE + LEVEL - 1, 2012 ), 'YYYY-MM-DD (IW-D)' ) AS "Date 2012",
TO_CHAR( SAME_DAY_AND_WEEK_OF_YEAR( SYSDATE + LEVEL - 1, 2011 ), 'YYYY-MM-DD (IW-D)' ) AS "Date 2011"
FROM DUAL
CONNECT BY LEVEL <= 7
<强> Results 强>:
| Date 2015 | Date 2014 | Date 2013 | Date 2012 | Date 2011 |
|-------------------|-------------------|-------------------|-------------------|-------------------|
| 2015-06-12 (24-6) | 2014-06-13 (24-6) | 2013-06-14 (24-6) | 2012-06-15 (24-6) | 2011-06-17 (24-6) |
| 2015-06-13 (24-7) | 2014-06-14 (24-7) | 2013-06-15 (24-7) | 2012-06-16 (24-7) | 2011-06-18 (24-7) |
| 2015-06-14 (24-1) | 2014-06-15 (24-1) | 2013-06-16 (24-1) | 2012-06-17 (24-1) | 2011-06-19 (24-1) |
| 2015-06-15 (25-2) | 2014-06-16 (25-2) | 2013-06-17 (25-2) | 2012-06-18 (25-2) | 2011-06-20 (25-2) |
| 2015-06-16 (25-3) | 2014-06-17 (25-3) | 2013-06-18 (25-3) | 2012-06-19 (25-3) | 2011-06-21 (25-3) |
| 2015-06-17 (25-4) | 2014-06-18 (25-4) | 2013-06-19 (25-4) | 2012-06-20 (25-4) | 2011-06-22 (25-4) |
| 2015-06-18 (25-5) | 2014-06-19 (25-5) | 2013-06-20 (25-5) | 2012-06-21 (25-5) | 2011-06-23 (25-5) |
答案 4 :(得分:0)
只需添加364,无论是否为闰年,例如
select
to_char(to_date('24/01/2016','dd/mm/yyyy'),'DAY DD/MM/YYYY') as CrossesLeapYear_PivotDate,
to_char(add_months(to_date('24/01/2016','dd/mm/yyyy'),12), 'DAY DD/MM/YYYY') as CrossesLeapYear_NextYearDate,
to_char(to_date('24/01/2016','dd/mm/yyyy') + 364, 'DAY DD/MM/YYYY') as CrossesLeapYear_SameNextYear,
to_char(to_date('24/01/2017','dd/mm/yyyy'),'DAY DD/MM/YYYY') as NoLeapYear_PivotDate,
to_char(add_months(to_date('24/01/2017','dd/mm/yyyy'),12), 'DAY DD/MM/YYYY') as NoLeapYear_NextYearDate,
to_char(to_date('24/01/2017','dd/mm/yyyy') + 364, 'DAY DD/MM/YYYY') as NoLeapYear_SameNextYear
from dual