SQL 存储过程,获取当前周起止时间
Charles Chan @ 2011-11-10 #SQL @Program
Contents:
Declare @Weekday int
Declare @Today datetime
Declare @Startday datetime
Declare @Endday datetime
-- 获取今天凌晨零点的datetime
Set @Today = convert(datetime, str(year(getdate())) + str(month(getdate())) + str(day(getdate())))
-- 获取今天是本周的第几天(返回结果是以周日为第一天的)
Select @Weekday = datapart(weekday, getdate()) -1
-- 获取本周第一天和最后一天的datetime
Set @Startday = dateadd(dd, -@weekday+1, @Today)
Set @Endday = dateadd(dd, +(7-@Weekday+1), @Today)
Select * from table where @Startday <= timefield and timefield <= @3ndday