declare @beginDate datetime
set @beginDate = '2002-12-01'
Select top 40 convert(varchar(10),dateadd(week,id,@beginDate)-1,120),id
from
(
Select top 40 (select sum(1) from sysobjects where name<= a.name) as id from sysobjects a
) bb
--下面是得到中文的星期
select dt,'星期'+substring('一二三四五六日',datepart(weekday,dt-1),1)
from(
select dt=convert(datetime,'2002-12-7')
union all select '2002-12-14'
union all select '2002-12-21'
union all select '2002-12-31'
union all select '2003-1-7'
) a
--每周的第一天为星期一的情况
select dt,datepart(weekday,dt-1)
from(
select dt=convert(datetime,'2002-12-7')
union all select '2002-12-14'
union all select '2002-12-21'
union all select '2002-12-31'
union all select '2003-1-7'
) a
--每周的第一天为星期日的情况
select dt,datepart(weekday,dt)
from(
select dt=convert(datetime,'2002-12-7')
union all select '2002-12-14'
union all select '2002-12-21'
union all select '2002-12-31'
union all select '2003-1-7'
) a
select dateadd(day,id,@a) 年,case datepart(weekday,dateadd(day,id,@a)) when 1 then '7' else cast(datepart(weekday,dateadd(day,id,@a))-1 as char(1)) end
from (
select (select count(*) from sysobjects where id<tem.id) id from sysobjects tem) aa where dateadd(day,id,@a)<=@b
是指定应返回的日期部分的参数。下表列出了 Microsoft® SQL Server™ 识别的日期部分和缩写。
期部分 缩写
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
Hour hh
minute mi, n
second ss, s
millisecond ms
Select dateadd(week,id,@a) 年,id+1 周数 from (select (select count(*) from sysobjects where id<a.id) id from sysobjects a) bb where dateadd(week,id,@a)<=@b