How to get week number of the month from the date in sql server 2008

后端 未结 22 1935
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 15:52

In SQL Statement in microsoft sql server, there is a built-in function to get week number but it is the week of the year.

Select DatePart(week, \'2012/11/30\         


        
22条回答
  •  一向
    一向 (楼主)
    2020-11-27 16:45

    No built-in function. It depends what you mean by week of month. You might mean whether it's in the first 7 days (week 1), the second 7 days (week 2), etc. In that case it would just be

    (DATEPART(day,@Date)-1)/7 + 1

    If you want to use the same week numbering as is used with DATEPART(week,), you could use the difference between the week numbers of the first of the month and the date in question (+1):

    (DATEPART(week,@Date)- DATEPART(week,DATEADD(m, DATEDIFF(m, 0, @Date), 0))) + 1

    Or, you might need something else, depending on what you mean by the week number.

提交回复
热议问题