2014-10-08 39 views
0

我想找到指定月份的第n天(例如: - 第一個星期天,第二個星期二,上週五等)2005.I曾使用發現我們每月的第N天在SQL Server 2005

DATEADD(WEEK, DATEDIFF(WEEK, 0, 
DATEADD(DAY, 6 - DATEPART(DAY, GETDATE()), GETDATE())), 6) 

在SQL Server它會返回一個月份的第一個星期日,但不知道如何找到每月的第N天我需要返回n天按照用戶的要求

可以在任何請幫忙找這個

任何幫助或建議將不勝感激

感謝和問候

+0

那你試試這麼遠嗎? (WEEK,DATEDIFF(WEEK,0,DATEADD(DAY,6 - DATEPART(DAY,GETDATE()),GETDATE())),6)\t我使用過,請使用代碼 – Paolo 2014-10-08 07:49:32

+0

@Paolo謝謝您的快速響應。這個查詢它將返回一個月的第一個星期日..但是dnt知道如何找到月的第n天..我需要根據用戶的請求返回第n天 – user3132347 2014-10-08 08:08:05

回答

0

許多數據倉庫都有日期維度。這個表格中包含諸如日期和星期幾之類的東西。它可以被索引並且具有相對較好的速度,因爲100年x 365天一年的數據並不多。

下面我使用一個計數表爲今年創建一個公用表表達式。我爲2014年11月創建了另一個。從那裏開始,我使用窗口函數根據發生次數來排列星期幾。

如果您確實擁有日期維度中的所有數據,則甚至可以將該出現編號放在該月份的表格中。

下面的例子翻出2014年11月

-- 
-- Why not use a date dimension if you need this functionality often? 
-- 

; 
with 
cte_Tally 
as 
(
SELECT ROW_NUMBER() over (order by (select 1)) as rn 
FROM master.sys.all_columns c 
), 
cte_Year 
as 
(
select 
    rn as my_doy_num, 
    dateadd(d, rn, '20140101') as my_day_dte, 
    month(dateadd(d, rn, '20140101')) as my_month_num, 
    case month(dateadd(d, rn, '20140101')) 
     when 1 then 'jan' 
     when 2 then 'feb' 
     when 3 then 'mar' 
     when 4 then 'apr' 
     when 5 then 'may' 
     when 6 then 'jun' 
     when 7 then 'jul' 
     when 8 then 'aug' 
     when 9 then 'sep' 
     when 10 then 'oct' 
     when 11 then 'nov' 
     when 12 then 'dec' 
     else '' 
    end as my_month_desc, 

    datepart(dw, dateadd(d, rn, '20140101')) as my_dow_num, 
    case datepart(dw, dateadd(d, rn, '20140101')) 
     when 1 then 'sun' 
     when 2 then 'mon' 
     when 3 then 'tue' 
     when 4 then 'wed' 
     when 5 then 'thu' 
     when 6 then 'fri' 
     when 7 then 'sat' 
     else '' 
    end as my_dow_desc 

from cte_Tally 
where rn < 365 
), 
cte_Nov_2014 
as 
(
select 
    my_day_dte, 
    my_month_desc, 
    my_dow_desc, 
    ROW_NUMBER() over (partition by my_dow_desc order by my_day_dte, my_dow_desc) as my_occurence 
from cte_Year 
where my_month_num = 11 
--order by my_dow_desc 
) 
select * 
from cte_Nov_2014 
where my_dow_desc = 'fri' and my_occurence = 3 
+0

謝謝..我會試試這個.. :) – user3132347 2014-10-11 07:11:55

-1
-- 1 find the first nth weekday (Mon/Tue/Wed/Thu/Fri/Sat/Sun) of the month where @dt is in 
-- for example, find the 5th Friday of the month of Aug, 2013 
declare @nth int=5, @dt datetime='2013-08-12'; 
declare @dw tinyint =5 -- 1=Mon,2= Tue,3=Wed, 4=Thur, 5=Fri, 6=Sat, 7=Sun 

select [1st_nth_wkday]=case when datepart(dw, dateadd(month, 
datediff(month,0,@dt),0)[email protected]@datefirst-1) >= @dw 
then dateadd(day, (@nth-1)*7 + (7-(datepart(dw, dateadd(month, 
datediff(month,0,@dt),0)[email protected]@datefirst-1)[email protected])), dateadd(month, 
datediff(month,0,@dt),0)) 
else dateadd(day, (@nth-1)*7 + (0-(datepart(dw, dateadd(month, 
datediff(month,0,@dt),0)[email protected]@datefirst-1)[email protected])), dateadd(month, 
datediff(month,0,@dt),0)) 
end 
go 


-- 2 find the last nth weekday (Mon/Tue/Wed/Thu/Fri/Sat/Sun) of the month 
where @dt is in 

-- find the 2nd last Sunday of current month 
declare @nth int=2, @dt datetime=current_timestamp; 
declare @dw tinyint =7 -- 1=Mon,2= Tue,3=Wed, 4=Thur, 5=Fri, 6=Sat, 7=Sun 

select [last_nth_wkday]=case when datepart(dw, dateadd(month, 
datediff(month,0,@dt)+1,0)[email protected]@datefirst-1) >= @dw 
then dateadd(day, -(@nth-1)*7 - (datepart(dw, dateadd(month, 
datediff(month,0,@dt)+1,0)[email protected]@datefirst-1)[email protected]), dateadd(month, 
datediff(month,0,@dt)+1,0)-1) 
else dateadd(day, -(@nth)*7 - (datepart(dw, dateadd(month, 
datediff(month,0,@dt)+1,0)[email protected]@datefirst-1)[email protected]), dateadd(monI th, 
datediff(month,0,@dt)+1,0)-1) 
end 
go 

月份其實我這個博客在這裏的第三個星期五:http://www.sqlservercentral.com/blogs/jeffrey_yao/2014/03/02/fun-in-datetime-calculations/