2017-04-18 82 views

回答

0

試試這個,我認爲這是你需要的全部:

set datefirst 6 

declare @dt datetime 
set @dt = '2017-03-04' 

select datepart(wk, @dt) - datepart(wk,dateadd(m, DATEDIFF(M, 0, @dt), 0)) + 1 as weekOfMonth 
0

您可以使用DATEFIRSTDATEADD。將DATEFIRST設置爲值6表示星期六。

試試這個(與你的datetime替換列yourDate):

SET DATEFIRST 6 
select datediff(week, dateadd(month, 0, yourDate), yourDate) + 1 
+1

感謝@Douek爲您的答案,但此句法向我顯示此錯誤消息:「數據類型datetime參數對dateadd函數的第二個參數無效。」 –

+0

@Koby Douek,交換yourDate和0在dateadd() –

+0

@MustaphaAitFadel請看更新的答案 –

0

您可以使用此查詢

DECLARE @currentdate date = '2017-04-01' 
select floor((datepart(day,@currentdate) - 1)/7) + 1 AS WeekOfMonth 

--- @currentdate date = '2017-04-01' -- return 1 
--- @currentdate date = '2017-04-07' -- return 1 
--- @currentdate date = '2017-04-08' -- return 2 
0

請使用以下查詢:

SELECT 'Week '+ CAST(FLOOR((datepart(day,@YourDate) - 1)/7) + 1 AS VARCHAR(10)) Currentweek 
相關問題