2013-01-21 105 views
4

當需要輸入B的值時,我需要在一個月內獲得第B個工作日。 例如,如果2013年1月份的b = 12,則結果值應該採用日期格式爲'17 -01-2013',因爲結果在排除週六,週日&當月假期後計算結果爲 。使用MySql獲取特定日期

我已經用下面的代碼&試圖在它的SQLserver工作正常,但是我發現很難在MySQL中執行它的某些功能不可用 作爲sqlserver的。

Declare 
@fromDate Date, 
@Daydiff int 

Set @fromDate ='01 jan 2013' 

Set @Daydiff=datediff(day, @fromdate, dateadd(month, 1, @fromdate)) 
Select * from 
(
Select 
    dateadd(day,DayNo,@fromDate) as Date, 
    dateName(weekday,(dateadd(day,DayNo,@fromDate))) As WeekDate, 
    Datename(month,(dateadd(day,DayNo,@fromDate))) as MonthName, 
    Row_number() Over (partition by (DatePart(month,(dateadd(day,DayNo,@fromDate)))) 
    order by (dateadd(day,DayNo,@fromDate))) as Business_day 
from 
    (Select top (@Daydiff) row_number() over(order by (select 1))-1 as DayNo 
    from sys.syscolumns a cross join sys.syscolumns b)Dates 
Where 
    dateName(weekday,(dateadd(day,DayNo,@fromDate))) Not In ('Saturday','Sunday') and 
    dateadd(day,DayNo,@fromDate) Not In (Select hdate from Holidays) 
)A 
Where Business_day=1 

假期是靜態的假期表,包含2013

假期表,我需要在MySQL類似的實例。 請幫助我。

+0

對於MYSQL,您可以使用[dayname](http://www.w3resource.com/mysql/date-and-time-functions/mysql-dayname-function.php)來替換'datename'。 (日期,日期,日期,@ fromDate))) order by(dateadd(day,DayNo,@ fromDate)))as Business_day' – bonCodigo

+0

雅。我們也處於同樣的境地。 – Bobby

+0

對於問題+1,如果所有查詢都不在ANSI中,並且某些功能在新平臺中不可用,則遷移可能會很痛苦;;) – bonCodigo

回答

2

SQLFiddle demo

如果您需要在末端偏移0的第一天集合。如果第二OFFSET 1中,如果15個集偏移14

select d 
FROM 
(
SELECT @row := @row + 1 as row, 
DATE_ADD('2013-01-01', INTERVAL @row-1 DAY) d 
from 
(SELECT @row := 0) r, 
(
select 1 n 
union all 
select 2 n 
union all 
select 3 n 
union all 
select 4 n 
union all 
select 5 n 
union all 
select 6 n 
) t1, 
(
select 1 n 
union all 
select 2 n 
union all 
select 3 n 
union all 
select 4 n 
union all 
select 5 n 
union all 
select 6 n 
) t2 
) num_seq 

where 
d<DATE_ADD('2013-01-01', INTERVAL 1 MONTH) 
and d not in (select hdate from Holidays) 
and DAYNAME(d) not in ('Saturday','Sunday') 
order by d 
LIMIT 1 OFFSET 20 

版無偏移和LIMIT。請參閱最新的where r=1這是第一天。如果您需要15個天更改爲where r=15

SQLFiddle demo

select d 
from 
(
select d,@r := @r + 1 as r 
FROM 
(SELECT @r := 0) r1, 
(
SELECT @row := @row + 1 as row, 
DATE_ADD('2013-01-01', INTERVAL @row-1 DAY) d 
from 
(SELECT @row := 0) r, 
(
select 1 n 
union all 
select 2 n 
union all 
select 3 n 
union all 
select 4 n 
union all 
select 5 n 
union all 
select 6 n 
) t1, 
(
select 1 n 
union all 
select 2 n 
union all 
select 3 n 
union all 
select 4 n 
union all 
select 5 n 
union all 
select 6 n 
) t2 
) num_seq 

where 
d<DATE_ADD('2013-01-01', INTERVAL 1 MONTH) 
and d not in (select hdate from Holidays) 
and DAYNAME(d) not in ('Saturday','Sunday') 
order by d 
) rTable 
where r=1 
+0

上述查詢僅跳過節假日表中列出的日期。但不是星期六和星期天。如果它跳過坐和太陽它將是完美的。 – Bobby

+1

我已修復此查詢並添加了'和DAYNAME(d)不在('Saturday','Sunday')' – valex

+0

非常感謝!小修改,如何通過偏移值作爲參數,是否有機會將偏移起始值從0更改爲1. 注意:其實我需要創建一個存儲過程,其日期和偏移值作爲參數 – Bobby

1

如何得到相同的結果時,只月份和年份作爲參數傳遞。因爲當我檢查代碼時......它的工作時間是相應月份的第一天,就像我輸入參數爲'2013-01-01'一樣,結果是絕對的,但如果日期爲'2013-01 -15'程序計算第1天的第15天並計算從那裏開始的第n天。

+0

Valex你可以看看這種情況。 – Bobby