1

我有日期和日期。现在我想知道那一天是第 1 周还是第 2 周、第 3 周或第 4 周。

假设我今天过了日期。然后它应该返回该月的“第一个星期二”。

如果我通过 2013 年 5 月 18 日,那么它应该返回“本月的第三个星期六”。如果我通过 2013 年 5 月 31 日,那么它应该返回“本月的第 5 个星期五”。

我怎样才能做到这一点?

4

5 回答 5

4
SELECT 
-- Do you need this: (DATEPART(WEEKDAY, GETDATE()) + @@DATEFIRST % 7)
CASE 1 + (DATEPART(DAY, GETDATE()) - 1) / 7
    WHEN 1 THEN '1St'
    WHEN 2 THEN '2nd'
    WHEN 3 THEN '3rd'
    WHEN 4 THEN '4th'
    ELSE '5th'
END
+ ' '
+ DATENAME(dw, GETDATE())
+ ' of the month'
于 2013-05-07T13:26:38.717 回答
2

尝试这个,

DECLARE @date DATETIME;
DECLARE @week INT;

SET @date = getdate()

SELECT @week = datepart(wk, @date) - datepart(wk, convert(DATETIME, cast(month(getdate()) AS VARCHAR) + '/01/' + cast(year(getdate()) AS VARCHAR)) + 1)

SELECT CASE WHEN @week = 1 THEN '1st ' WHEN @week = 2 THEN '2nd ' WHEN @week = 3 THEN '3RD ' ELSE '4th ' END + DATENAME(weekday, @date) + ' of the month'
于 2013-05-07T12:45:29.900 回答
2

干得好

DECLARE @date DATETIME;

SET @date = '2013-08-30'
select 
case when DATEPART(dd,@date)<=7 then '1st '+datename(DW,@date)
when DATEPART(dd,@date)>7 and DATEPART(dd,@date)<=14 then '2nd '+datename(DW,@date)
when DATEPART(dd,@date)>14 and DATEPART(dd,@date)<=21 then '3rd '+datename(DW,@date)
when DATEPART(dd,@date)>21 and DATEPART(dd,@date)<=28 then '4th '+datename(DW,@date)
when DATEPART(dd,@date)>28 and DATEPART(dd,@date)<=31 then '5th '+datename(DW,@date) end
于 2013-05-07T13:15:36.183 回答
1

Well , if day XXX's number is between 1 and 7, it's the first XXX of the month , if it's between 8 and 14 , it's the second XXX of the month, if it's between 15 and 21 , it's the 3rd XXX of the month, if it's between 22 and 28, it's the 4th XXX of the month , and if it's over 28 it's the 5th XXX of the month...

于 2013-05-07T12:25:42.293 回答
1

另一种选择

DECLARE @date datetime = GETDATE()
SELECT CASE DATEPART(ww, @date) - (DATEPART(ww, DATEADD(DAY, 1 - DAY(@date), @date)) 
       - CASE WHEN DATEPART(dw, DATEADD(dd, 1 - DAY(@date), @date)) <= DATEPART(dw, @date) THEN 1 ELSE 0 END)
         WHEN 1 THEN '1st '
         WHEN 2 THEN '2nd '
         WHEN 3 THEN '3rd '
         WHEN 4 THEN '4th '
         WHEN 5 THEN '5th ' END + DATENAME(dw, @date) + ' of the month' 

SQLFiddle上的演示

于 2013-05-07T14:06:14.517 回答