您是否尝试过内置的 DATE_PART 周,也许您可以使用它?
SELECT DATEPART( WEEK , 'May 2, 2012')
http://www.sqlfiddle.com/#!3/e93ae/2
关于 :
对于我在此范围内获取的第一条记录,此处的周数应从 1 开始。这不是正常的日历周数
试试这个:
SELECT
z.filter_date, x.d,
(datediff(day,z.filter_date, x.d) / 7) + 1 as week_number
from x
cross join (select convert(date,'march 7, 2012') as filter_date) as z
where x.d >= z.filter_date
数据:
create table x(i int identity(1,1) primary key, d date);
insert into x(d) values
('February 2, 2012'),
('February 5, 2012'),
('March 7, 2012'),
('April 8, 2012'),
('May 9, 2012'),
('June 9, 2012');
输出:
FILTER_DATE D WEEK_NUMBER
2012-03-07 2012-03-07 1
2012-03-07 2012-04-08 5
2012-03-07 2012-05-09 10
2012-03-07 2012-06-09 14
现场测试:http ://www.sqlfiddle.com/#!3/e4837/12