开发者

calculate week number within a date range

开发者 https://www.devze.com 2023-02-24 09:34 出处:网络
How will I calculate week number within a date range ? Here my week starts开发者_如何学C from Saturday to Friday. And I have a start date and end date. With this, how will i calculate using SQL Query

How will I calculate week number within a date range ? Here my week starts开发者_如何学C from Saturday to Friday. And I have a start date and end date. With this, how will i calculate using SQL Query ?


Use DATEPART to get the Week number, and DATEFIRST to set the first day of the week. (See http://msdn.microsoft.com/en-us/library/ms181598.aspx)

Example to get all week numbers in range with Saturday as the first day of week.

SET DATEFIRST 6
SELECT DISTINCT 
    DATEPART(WEEK, createDate) 
FROM 
    tblUser
WHERE
    createDate > '2005-01-01' AND createDate < '2011-01-01'
ORDER BY
    DATEPART(WEEK, createDate)


You can use this and your SQL work operates well:

SET LANGUAGE us_english -- with your language 
GO
0

精彩评论

暂无评论...
验证码 换一张
取 消