How to get last date of the lastweek in sql? I mean last sunday date using query?
如何在sql中获取上周的最后日期?我是说上星期天的约会用查询?
10 个解决方案
#1
33
Regardless of the actual DATEFIRST setting, the last Sunday could be found like this:
不管实际的日期优先设置是什么,最后一个星期日可以这样找到:
SELECT DATEADD(day,
-1 - (DATEPART(weekday, GETDATE()) + @@DATEFIRST - 2) % 7,
GETDATE()
) AS LastSunday
Replace GETDATE()
with a parameter @date
to get the last Sunday before a particular date.
将GETDATE()替换为参数@date,以便在特定日期之前获得最后一个星期日。
#2
18
Last Sunday (Which is the end of "last week")
上周日(也就是“上周”的结尾)
SELECT DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 6) AS LAST_SUNDAY
This Week (Assuming Mon-Sun Week Format)
这周(假设周一至周日的格式)
SELECT DATEADD(wk, DATEDIFF(wk, 7, CURRENT_TIMESTAMP), 7) AS START_OF_WEEK
SELECT DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 6 + 7) AS END_OF_WEEK
Results
结果
START_OF_WEEK
-----------------------
2011-05-02 00:00:00.000
END_OF_WEEK
-----------------------
2011-05-08 00:00:00.000
Examples to explain the voodoo (Use this to change above SQL to your desired Week Starting and Week Ending day-of-week)
解释voodoo的示例(使用此示例将上面的SQL更改为所需的开始周和结束周)
- The examples below locate days of the week within the current week (Sunday to Saturday)
- 以下的例子在本星期(星期日至星期六)的星期内
- If the actual END_OF_WEEK is next Sun-Sat week, then you need to +7 to this week's value. (See the END_OF_WEEK example above.)
- 如果实际的END_OF_WEEK是下一个Sun-Sat周,那么需要+7到这个周的值。(参见上面的END_OF_WEEK示例。)
SQL Below
下面的SQL
SELECT DATEADD(wk, DATEDIFF(wk, -2, CURRENT_TIMESTAMP), -2) AS DAY_OF_WEEK /* Saturday */
SELECT DATEADD(wk, DATEDIFF(wk, -1, CURRENT_TIMESTAMP), -1) AS DAY_OF_WEEK /* Sunday */
SELECT DATEADD(wk, DATEDIFF(wk, 0, CURRENT_TIMESTAMP), 0) AS DAY_OF_WEEK /* Monday */
SELECT DATEADD(wk, DATEDIFF(wk, 1, CURRENT_TIMESTAMP), 1) AS DAY_OF_WEEK /* Tuesday */
SELECT DATEADD(wk, DATEDIFF(wk, 2, CURRENT_TIMESTAMP), 2) AS DAY_OF_WEEK /* Wednesday */
SELECT DATEADD(wk, DATEDIFF(wk, 3, CURRENT_TIMESTAMP), 3) AS DAY_OF_WEEK /* Thursday */
SELECT DATEADD(wk, DATEDIFF(wk, 4, CURRENT_TIMESTAMP), 4) AS DAY_OF_WEEK /* Friday */
SELECT DATEADD(wk, DATEDIFF(wk, 5, CURRENT_TIMESTAMP), 5) AS DAY_OF_WEEK /* Saturday */
SELECT DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 6) AS DAY_OF_WEEK /* Sunday */
SELECT DATEADD(wk, DATEDIFF(wk, 7, CURRENT_TIMESTAMP), 7) AS DAY_OF_WEEK /* Monday */
SELECT DATEADD(wk, DATEDIFF(wk, 8, CURRENT_TIMESTAMP), 8) AS DAY_OF_WEEK /* Tuesday */
SELECT DATEADD(wk, DATEDIFF(wk, 9, CURRENT_TIMESTAMP), 9) AS DAY_OF_WEEK /* Wednesday */
SELECT DATEADD(wk, DATEDIFF(wk, 10, CURRENT_TIMESTAMP), 10) AS DAY_OF_WEEK /* Thursday */
SELECT DATEADD(wk, DATEDIFF(wk, 11, CURRENT_TIMESTAMP), 11) AS DAY_OF_WEEK /* Friday */
SELECT DATEADD(wk, DATEDIFF(wk, 12, CURRENT_TIMESTAMP), 12) AS DAY_OF_WEEK /* Saturday */
etc...
#3
3
Here is a great article on how to do this:
这里有一篇关于如何做到这一点的文章:
http://www.objectreference.net/post/SQL-Find-last-week-date-range.aspx
http://www.objectreference.net/post/SQL-Find-last-week-date-range.aspx
You would want to use the @StartOfPrevWeek variable.
您需要使用@StartOfPrevWeek变量。
#4
1
DECLARE @LastSunday DATETIME
-- This will get the previous Sunday with time as 23:59:59
SELECT @LastSunday = Dateadd(SECOND, -1, Dateadd(WK, Datediff(WK, 6,
CURRENT_TIMESTAMP)
, 7))
SELECT @LastSunday
-- This gets the monday prior to it and time of 00:00:00
SELECT Dateadd(SECOND, 1, Dateadd(DAY, -7, @LastSunday))
-- This will make you time spans between eg, Monday 16/07/2012 00:00:00 through to Sunday 22/07/2012 23:59:59
-- Then use them in your WHERE clause like this
-- SELECT X,Y,Z From SomeTable
-- WHERE DateField BETWEEN @PreviousMondayToLastSunday AND @LastSunday
#5
1
To get the previous sunday, or today if today is sunday, try this
如果今天是星期天,那就试试这个。
DATEADD(day,- (DATEPART(dw,getdate()) + @@DATEFIRST -1) % 7, getdate())
#6
0
The SQL is more straightforward with a suitable calendar table. No voodoo.
使用合适的日历表,SQL更简单。没有巫术。
select max(cal_date) end_of_last_week
from calendar
where (cal_date < current_date and day_of_week = 'Sun');
end_of_last_week
--
2011-05-01
#7
0
SELECT (DATEADD(DAY, ((DATEPART(dw, @Date) - 1) * -1), @Date))
#8
0
This will get you the next and precious Friday from a given date and time
这将使你从给定的日期和时间得到下一个和宝贵的星期五
DECLARE @PREVIOUS int, @dtmStart datetime,@dtmEnd datetime, @NEXT int;
SET @dtmStart = '12/10/2013';
SET @dtmEnd = '12/11/2013';
select @PREVIOUS = datepart(dw,@dtmStart)
WHILE @PREVIOUS <> 6
BEGIN
SET @dtmStart = DATEADD(day , -1 ,@dtmStart)
SET @PREVIOUS = datepart(dw,@dtmStart)
CONTINUE
END
select @dtmStart
SELECT @NEXT = DATEPART(dw, @dtmEnd)
WHILE @NEXT <> 6
BEGIN
SET @dtmEnd = DATEADD(day , 1 ,@dtmEnd)
SET @NEXT = datepart(dw,@dtmEnd)
CONTINUE
END
select @dtmEnd
#9
-1
Here is the code to get the date of last Saturday. This method is independent of settings of the database.
这是得到上星期六日期的密码。此方法独立于数据库的设置。
declare @lastSaturday date,
@today date,
@todayName varchar(20);
select @todayName = datename(weekday, getdate()), @today = getdate();
select
case @todayName
when 'Saturday' then @today
when 'Sunday' then dateadd(day,-1,@today)
when 'Monday' then dateadd(day,-2,@today)
when 'Tuesday' then dateadd(day,-3,@today)
when 'Wednesday' then dateadd(day,-4,@today)
when 'Thursday' then dateadd(day,-5,@today)
when 'Friday' then dateadd(day,-6,@today)
end as LastSaturday;
#10
-2
SET @EndDate = GETDATE()-DatePart(dw, GETDATE());
#1
33
Regardless of the actual DATEFIRST setting, the last Sunday could be found like this:
不管实际的日期优先设置是什么,最后一个星期日可以这样找到:
SELECT DATEADD(day,
-1 - (DATEPART(weekday, GETDATE()) + @@DATEFIRST - 2) % 7,
GETDATE()
) AS LastSunday
Replace GETDATE()
with a parameter @date
to get the last Sunday before a particular date.
将GETDATE()替换为参数@date,以便在特定日期之前获得最后一个星期日。
#2
18
Last Sunday (Which is the end of "last week")
上周日(也就是“上周”的结尾)
SELECT DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 6) AS LAST_SUNDAY
This Week (Assuming Mon-Sun Week Format)
这周(假设周一至周日的格式)
SELECT DATEADD(wk, DATEDIFF(wk, 7, CURRENT_TIMESTAMP), 7) AS START_OF_WEEK
SELECT DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 6 + 7) AS END_OF_WEEK
Results
结果
START_OF_WEEK
-----------------------
2011-05-02 00:00:00.000
END_OF_WEEK
-----------------------
2011-05-08 00:00:00.000
Examples to explain the voodoo (Use this to change above SQL to your desired Week Starting and Week Ending day-of-week)
解释voodoo的示例(使用此示例将上面的SQL更改为所需的开始周和结束周)
- The examples below locate days of the week within the current week (Sunday to Saturday)
- 以下的例子在本星期(星期日至星期六)的星期内
- If the actual END_OF_WEEK is next Sun-Sat week, then you need to +7 to this week's value. (See the END_OF_WEEK example above.)
- 如果实际的END_OF_WEEK是下一个Sun-Sat周,那么需要+7到这个周的值。(参见上面的END_OF_WEEK示例。)
SQL Below
下面的SQL
SELECT DATEADD(wk, DATEDIFF(wk, -2, CURRENT_TIMESTAMP), -2) AS DAY_OF_WEEK /* Saturday */
SELECT DATEADD(wk, DATEDIFF(wk, -1, CURRENT_TIMESTAMP), -1) AS DAY_OF_WEEK /* Sunday */
SELECT DATEADD(wk, DATEDIFF(wk, 0, CURRENT_TIMESTAMP), 0) AS DAY_OF_WEEK /* Monday */
SELECT DATEADD(wk, DATEDIFF(wk, 1, CURRENT_TIMESTAMP), 1) AS DAY_OF_WEEK /* Tuesday */
SELECT DATEADD(wk, DATEDIFF(wk, 2, CURRENT_TIMESTAMP), 2) AS DAY_OF_WEEK /* Wednesday */
SELECT DATEADD(wk, DATEDIFF(wk, 3, CURRENT_TIMESTAMP), 3) AS DAY_OF_WEEK /* Thursday */
SELECT DATEADD(wk, DATEDIFF(wk, 4, CURRENT_TIMESTAMP), 4) AS DAY_OF_WEEK /* Friday */
SELECT DATEADD(wk, DATEDIFF(wk, 5, CURRENT_TIMESTAMP), 5) AS DAY_OF_WEEK /* Saturday */
SELECT DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 6) AS DAY_OF_WEEK /* Sunday */
SELECT DATEADD(wk, DATEDIFF(wk, 7, CURRENT_TIMESTAMP), 7) AS DAY_OF_WEEK /* Monday */
SELECT DATEADD(wk, DATEDIFF(wk, 8, CURRENT_TIMESTAMP), 8) AS DAY_OF_WEEK /* Tuesday */
SELECT DATEADD(wk, DATEDIFF(wk, 9, CURRENT_TIMESTAMP), 9) AS DAY_OF_WEEK /* Wednesday */
SELECT DATEADD(wk, DATEDIFF(wk, 10, CURRENT_TIMESTAMP), 10) AS DAY_OF_WEEK /* Thursday */
SELECT DATEADD(wk, DATEDIFF(wk, 11, CURRENT_TIMESTAMP), 11) AS DAY_OF_WEEK /* Friday */
SELECT DATEADD(wk, DATEDIFF(wk, 12, CURRENT_TIMESTAMP), 12) AS DAY_OF_WEEK /* Saturday */
etc...
#3
3
Here is a great article on how to do this:
这里有一篇关于如何做到这一点的文章:
http://www.objectreference.net/post/SQL-Find-last-week-date-range.aspx
http://www.objectreference.net/post/SQL-Find-last-week-date-range.aspx
You would want to use the @StartOfPrevWeek variable.
您需要使用@StartOfPrevWeek变量。
#4
1
DECLARE @LastSunday DATETIME
-- This will get the previous Sunday with time as 23:59:59
SELECT @LastSunday = Dateadd(SECOND, -1, Dateadd(WK, Datediff(WK, 6,
CURRENT_TIMESTAMP)
, 7))
SELECT @LastSunday
-- This gets the monday prior to it and time of 00:00:00
SELECT Dateadd(SECOND, 1, Dateadd(DAY, -7, @LastSunday))
-- This will make you time spans between eg, Monday 16/07/2012 00:00:00 through to Sunday 22/07/2012 23:59:59
-- Then use them in your WHERE clause like this
-- SELECT X,Y,Z From SomeTable
-- WHERE DateField BETWEEN @PreviousMondayToLastSunday AND @LastSunday
#5
1
To get the previous sunday, or today if today is sunday, try this
如果今天是星期天,那就试试这个。
DATEADD(day,- (DATEPART(dw,getdate()) + @@DATEFIRST -1) % 7, getdate())
#6
0
The SQL is more straightforward with a suitable calendar table. No voodoo.
使用合适的日历表,SQL更简单。没有巫术。
select max(cal_date) end_of_last_week
from calendar
where (cal_date < current_date and day_of_week = 'Sun');
end_of_last_week
--
2011-05-01
#7
0
SELECT (DATEADD(DAY, ((DATEPART(dw, @Date) - 1) * -1), @Date))
#8
0
This will get you the next and precious Friday from a given date and time
这将使你从给定的日期和时间得到下一个和宝贵的星期五
DECLARE @PREVIOUS int, @dtmStart datetime,@dtmEnd datetime, @NEXT int;
SET @dtmStart = '12/10/2013';
SET @dtmEnd = '12/11/2013';
select @PREVIOUS = datepart(dw,@dtmStart)
WHILE @PREVIOUS <> 6
BEGIN
SET @dtmStart = DATEADD(day , -1 ,@dtmStart)
SET @PREVIOUS = datepart(dw,@dtmStart)
CONTINUE
END
select @dtmStart
SELECT @NEXT = DATEPART(dw, @dtmEnd)
WHILE @NEXT <> 6
BEGIN
SET @dtmEnd = DATEADD(day , 1 ,@dtmEnd)
SET @NEXT = datepart(dw,@dtmEnd)
CONTINUE
END
select @dtmEnd
#9
-1
Here is the code to get the date of last Saturday. This method is independent of settings of the database.
这是得到上星期六日期的密码。此方法独立于数据库的设置。
declare @lastSaturday date,
@today date,
@todayName varchar(20);
select @todayName = datename(weekday, getdate()), @today = getdate();
select
case @todayName
when 'Saturday' then @today
when 'Sunday' then dateadd(day,-1,@today)
when 'Monday' then dateadd(day,-2,@today)
when 'Tuesday' then dateadd(day,-3,@today)
when 'Wednesday' then dateadd(day,-4,@today)
when 'Thursday' then dateadd(day,-5,@today)
when 'Friday' then dateadd(day,-6,@today)
end as LastSaturday;
#10
-2
SET @EndDate = GETDATE()-DatePart(dw, GETDATE());