zeerd's blog     Article     Search     About     Tags     Pebble     Feed

闲来生雅趣,无事乐逍遥。对窗相望雪,一盏茶香飘。

SQL 存储过程,获取当前周起止时间

#SQL


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