如何在sql server 2008中获得一周的最后一天和一周的最后一天?

问题描述:

如果我们输入一周中的任何一天,如何获得星期的第一天和最后一天?

How to get the first day of the week and last day of the week when we input any one day of a week?

例如,如果我们输入日期那么应该显示第一个(星期一)和最后一个(星期五)日。
,如果我们进入24-jan-2014,那么应该显示20-jan-2014和24-jan-2014。

For example if we enter a date then the first(Monday) and last (Friday) day should be displayed. that is if we enter 24-jan-2014 then 20-jan-2014 and 24-jan-2014 should be displayed.

关心

可以这样做:

DECLARE @yourdate date = getdate()
Select dateadd(ww, datediff(ww, 0, @yourdate), 0)
Select dateadd(ww, datediff(ww, 0, @yourdate), 4)

您将 @yourdate 设置为你想要的日期第一个SELECT将给你第一天,第二个SELECT将给你最后一个日期

You set @yourdate to the date you want. The first SELECT will give you the first day and the second SELECT will give you the last date