如何查找月份的第4个星期六?

时间:2014-01-17 11:29:45

标签: sql oracle

请告诉我如何在oracle找到第四个星期六。假设我们把“03/02/1999”然后写一个sql查询来查找月份的第四个星期六。

4 个答案:

答案 0 :(得分:4)

在Oracle中,您可以使用:

NEXT_DAY(TRUNC(TO_DATE('1999-02-03','yyyy-mm-dd'), 'MM')-1, 'Saturday')+3*7

答案 1 :(得分:0)

 select next_day(trunc(to_date('1999-02-             03','yyyy-mm-dd'),'mm')-1,'saturday')+21 
 from dual;

答案 2 :(得分:0)

TRUNC( date, [fmt] )接受一个可选的第二个参数,它确定你截断到的时间部分;这样:

TRUNC( your_date, 'mm' )

将截断到该月的第一天,如果您从中减去一天,那么您将获得上个月的最后一天:

TRUNC( your_date, 'mm' ) - INTERVAL '1' DAY

或者更简单地说,

TRUNC( your_date, 'mm' ) - 1

NEXT_DAY( date, char)将从给定日期获得第二个参数指定的第二天。结合这两个函数将为您提供该月的第一个指定日期:

NEXT_DAY( TRUNC( your_date, 'mm' ) - 1, day_of_week )

添加两周会给你一个月的第四天:

NEXT_DAY( TRUNC( your_date, 'mm' ) - 1, 'Saturday' ) + INTERVAL '21' DAY

举个例子:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE dates ( your_date ) AS
SELECT TO_DATE( '2014-' || LPAD( LEVEL, 2, '0' ) || '-01', 'YYYY-MM-DD' )
FROM   DUAL
CONNECT BY LEVEL < 12
/

查询1

SELECT NEXT_DAY( TRUNC( your_date, 'mm') - INTERVAL '1' DAY, 'Saturday') + INTERVAL '21' DAY AS fourth_saturday
FROM   dates

<强> Results

|                  FOURTH_SATURDAY |
|----------------------------------|
|   January, 25 2014 00:00:00+0000 |
|  February, 22 2014 00:00:00+0000 |
|     March, 22 2014 00:00:00+0000 |
|     April, 26 2014 00:00:00+0000 |
|       May, 24 2014 00:00:00+0000 |
|      June, 28 2014 00:00:00+0000 |
|      July, 26 2014 00:00:00+0000 |
|    August, 23 2014 00:00:00+0000 |
| September, 27 2014 00:00:00+0000 |
|   October, 25 2014 00:00:00+0000 |
|  November, 22 2014 00:00:00+0000 |

您还可以将代码包装在(更通用的)函数中:

CREATE FUNCTION nth_day_of_month (
  month       DATE,
  day_of_week VARCHAR2,
  nth         NUMBER
) RETURN DATE DETERMINISTIC
IS
BEGIN
  RETURN NEXT_DAY( TRUNC( month, 'mm' ) - INTERVAL '1' DAY, day_of_week ) + (nth - 1) * 7;
END nth_day_of_month;
/

查询2

SELECT TO_CHAR( nth_day_of_month( your_date, 'Monday', 1 ), 'DAY YYYY-MM-DD' ) AS first_monday,
       TO_CHAR( nth_day_of_month( your_date, 'Saturday', 4 ), 'DAY YYYY-MM-DD' ) AS fourth_saturday
FROM   dates

<强> Results

|      FIRST_MONDAY |     FOURTH_SATURDAY |
|-------------------|---------------------|
| MONDAY 2014-01-06 | SATURDAY 2014-01-25 |
| MONDAY 2014-02-03 | SATURDAY 2014-02-22 |
| MONDAY 2014-03-03 | SATURDAY 2014-03-22 |
| MONDAY 2014-04-07 | SATURDAY 2014-04-26 |
| MONDAY 2014-05-05 | SATURDAY 2014-05-24 |
| MONDAY 2014-06-02 | SATURDAY 2014-06-28 |
| MONDAY 2014-07-07 | SATURDAY 2014-07-26 |
| MONDAY 2014-08-04 | SATURDAY 2014-08-23 |
| MONDAY 2014-09-01 | SATURDAY 2014-09-27 |
| MONDAY 2014-10-06 | SATURDAY 2014-10-25 |
| MONDAY 2014-11-03 | SATURDAY 2014-11-22 |

答案 3 :(得分:-1)

试试这个

DECLARE @StartDate DATETIME;
SELECT @StartDate = DATEADD(year, DATEDIFF(year,0,GETDATE()), 0);
 -- This clause is just to get some numbers: a Tally table, for joining to get a range of dates
WITH NumDays AS ( 
SELECT TOP 365 -- one year of days
        ROW_NUMBER() OVER (ORDER BY sc1.ID) AS N
   FROM Master.dbo.SysColumns sc1,
        Master.dbo.SysColumns sc2
UNION SELECT 0 -- just to get a zero in the results
 ), DateRange AS (
SELECT N,
DATEADD(day, N, @StartDate) AS [Date],
DATEPART(dw,DATEADD(day, N, @StartDate)) AS [Day],
DATENAME(dw,DATEADD(day, N, @StartDate)) AS [DayName],
DATENAME(week,DATEADD(day, N, @StartDate)) AS [WeekNo]
FROM NumDays
 )
 SELECT * 
 FROM DateRange
 WHERE [DayName] = 'Saturday'
 AND WeekNo in (4)

在这里,您可以将weekNo逗号分开。假设您要查找第2个和第4个星期六,然后添加此WeekNo in (2,4)而不是WeekNo in (4)