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

时间:2022-10-21 10:14:53

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

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

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.

例如,如果我们输入日期,则应显示第一天(星期一)和最后一天(星期五)。也就是说,如果我们进入2014年1月24日,则应显示2014年1月20日和2014年1月24日。

Regards

3 个解决方案

#1


14  

Here's how you can do it:

以下是您可以这样做的方法:

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

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

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

#2


2  

Try this

SELECT DATEADD(wk, DATEDIFF(wk, 6, '5/13/2005'), 6)
SELECT DATEADD(wk, DATEDIFF(wk, 5, '5/13/2005'), 5)

(Or)

Declare @Date datetime
Det @Date = '2012-04-12'
Delect @Date - DATEPART(dw, @Date) + 1 FirstDateOfWeek,
       @Date + (7 - DATEPART(dw, @Date)) LastDateOfWeek

#3


2  

This solves it and also wraps around year ends:

这解决了它并且也包裹了年终:

SELECT DATEADD(wk, DATEDIFF(d, 0, '01 January 2017') / 7, 0)

SELECT DATEADD(周刊,DATEDIFF(d,0,'2017年1月1日')/ 7,0)

#1


14  

Here's how you can do it:

以下是您可以这样做的方法:

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

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

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

#2


2  

Try this

SELECT DATEADD(wk, DATEDIFF(wk, 6, '5/13/2005'), 6)
SELECT DATEADD(wk, DATEDIFF(wk, 5, '5/13/2005'), 5)

(Or)

Declare @Date datetime
Det @Date = '2012-04-12'
Delect @Date - DATEPART(dw, @Date) + 1 FirstDateOfWeek,
       @Date + (7 - DATEPART(dw, @Date)) LastDateOfWeek

#3


2  

This solves it and also wraps around year ends:

这解决了它并且也包裹了年终:

SELECT DATEADD(wk, DATEDIFF(d, 0, '01 January 2017') / 7, 0)

SELECT DATEADD(周刊,DATEDIFF(d,0,'2017年1月1日')/ 7,0)