0

我需要找出给定月份的第一个/第二个/第三个/第四个/最后一个工作日。
我还需要周末来安排复发。

我可以找到第一个工作日:

SELECT CASE Dayofweek('2012-11-04') 
         WHEN 1 THEN Date_add('2012-11-04', INTERVAL 1 DAY) 
         WHEN 7 THEN Date_add('2012-11-04', INTERVAL 2 DAY) 
         ELSE '2012-11-04' 
       END AS dateStart 

同样,我需要获得第二个三分之一,等等。

4

2 回答 2

0

On SQL Server, DATEPART(dw, your_date) will tell you if it is a weekend. Usually 1 means saturday and 7 means sunday but it can change depending of the server configuration. Read about the datepart function to understand how it works

于 2012-10-18T14:26:35.650 回答
0

in MySQL:

CREATE TABLE calendar
(
  date date not null,
  isBus bit
);
ALTER TABLE calendar
ADD CONSTRAINT PK_calendar
PRIMARY KEY(date);


INSERT INTO calendar VALUES ('2012-10-01', 1);
INSERT INTO calendar VALUES ('2012-10-02', 1);
INSERT INTO calendar VALUES ('2012-10-03', 1);
INSERT INTO calendar VALUES ('2012-10-04', 1);
INSERT INTO calendar VALUES ('2012-10-05', 1);
INSERT INTO calendar VALUES ('2012-10-06', 0);
INSERT INTO calendar VALUES ('2012-10-07', 0);
INSERT INTO calendar VALUES ('2012-10-08', 1);
INSERT INTO calendar VALUES ('2012-10-09', 1);
INSERT INTO calendar VALUES ('2012-10-10', 1);
INSERT INTO calendar VALUES ('2012-10-11', 1);
INSERT INTO calendar VALUES ('2012-10-12', 1);
INSERT INTO calendar VALUES ('2012-10-13', 0);
INSERT INTO calendar VALUES ('2012-10-14', 0);
INSERT INTO calendar VALUES ('2012-10-15', 1);
INSERT INTO calendar VALUES ('2012-10-16', 1);
INSERT INTO calendar VALUES ('2012-10-17', 1);
INSERT INTO calendar VALUES ('2012-10-18', 1);
INSERT INTO calendar VALUES ('2012-10-19', 1);
INSERT INTO calendar VALUES ('2012-10-20', 0);
INSERT INTO calendar VALUES ('2012-10-21', 0);
INSERT INTO calendar VALUES ('2012-10-22', 1);
INSERT INTO calendar VALUES ('2012-10-23', 1);
INSERT INTO calendar VALUES ('2012-10-24', 1);
INSERT INTO calendar VALUES ('2012-10-25', 1);
INSERT INTO calendar VALUES ('2012-10-26', 1);
INSERT INTO calendar VALUES ('2012-10-27', 0);
INSERT INTO calendar VALUES ('2012-10-28', 0);
INSERT INTO calendar VALUES ('2012-10-29', 1);
INSERT INTO calendar VALUES ('2012-10-30', 1);
INSERT INTO calendar VALUES ('2012-10-31', 1);
INSERT INTO calendar VALUES ('2012-11-01', 1);
INSERT INTO calendar VALUES ('2012-11-02', 1);
INSERT INTO calendar VALUES ('2012-11-03', 0);
INSERT INTO calendar VALUES ('2012-11-04', 0);
INSERT INTO calendar VALUES ('2012-11-05', 1);
INSERT INTO calendar VALUES ('2012-11-06', 1);
INSERT INTO calendar VALUES ('2012-11-07', 1);
INSERT INTO calendar VALUES ('2012-11-08', 1);
INSERT INTO calendar VALUES ('2012-11-09', 1);
INSERT INTO calendar VALUES ('2012-11-10', 0);
INSERT INTO calendar VALUES ('2012-11-11', 0);
INSERT INTO calendar VALUES ('2012-11-12', 1);
INSERT INTO calendar VALUES ('2012-11-13', 1);
INSERT INTO calendar VALUES ('2012-11-14', 1);
INSERT INTO calendar VALUES ('2012-11-15', 1);
INSERT INTO calendar VALUES ('2012-11-16', 1);
INSERT INTO calendar VALUES ('2012-11-17', 0);
INSERT INTO calendar VALUES ('2012-11-18', 0);
INSERT INTO calendar VALUES ('2012-11-19', 1);
INSERT INTO calendar VALUES ('2012-11-20', 1);
INSERT INTO calendar VALUES ('2012-11-21', 1);
INSERT INTO calendar VALUES ('2012-11-22', 1);
INSERT INTO calendar VALUES ('2012-11-23', 1);
INSERT INTO calendar VALUES ('2012-11-24', 0);
INSERT INTO calendar VALUES ('2012-11-25', 0);
INSERT INTO calendar VALUES ('2012-11-26', 1);
INSERT INTO calendar VALUES ('2012-11-27', 1);
INSERT INTO calendar VALUES ('2012-11-28', 1);
INSERT INTO calendar VALUES ('2012-11-29', 1);
INSERT INTO calendar VALUES ('2012-11-30', 1);

then to get all Thursdays (5) in October:

select @rowid:=@rowid+1, calendar.* from calendar, (SELECT @rowid:=0) as init 
where dayofweek(date) = 5
and month(date) = 10
order by date;

to get the 2nd Thursday in October:

select * from
(select @rowid:=@rowid+1 as seqNo, calendar.* from calendar, (SELECT @rowid:=0) as init 
where dayofweek(date) = 5
and month(date) = 10
order by date) x
where seqNo = 2;  
于 2012-10-18T14:50:17.867 回答