我想查找上周的日期
SELECT *
FROM Table
Where [Date] Between '04-Jan-2016' AND '15-Jan-2016'
每次运行SQL Query时,都需要显示last week
。
答案 0 :(得分:4)
希望以下是你想要的,
--START OF LAST WEEK
SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0)
--END OF LAST WEEK
SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 4)
DECLARE @input varchar(10)
--SET @input = '01/10/2016'
SET @input = GETDATE()
--START OF LAST WEEK
SELECT DATEADD(wk, DATEDIFF(wk, 6,
CASE DATEPART(dw,@input)
WHEN 1 THEN DATEADD(d,-1,@input)
ELSE @input
END
), 0)
--END OF LAST WEEK
SELECT DATEADD(wk, DATEDIFF(wk, 6,
CASE DATEPART(dw,@input)
WHEN 1 THEN DATEADD(d,-1,@input)
ELSE @input
END
), 4)
答案 1 :(得分:2)
---要在SQL Server中获取上一周的第一天,请使用以下代码:
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0)
- 要获得前一周的最后一天:
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),4)
答案 2 :(得分:2)
SELECT *
FROM Table
Where [Date] Between DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0)
AND DATEADD(wk,DATEDIFF(wk,7,GETDATE()),4)
答案 3 :(得分:0)
我需要从上周一的00:00:00到前一个周日的23:59:59的范围。这是您可以获得的方式:
boolean
没有设置变量的情况相同:
SET @THIS_MONDAY = SUBDATE(DATE(NOW()), WEEKDAY(NOW()));
SELECT CONCAT(SUBDATE(@THIS_MONDAY, 7), ' 00:00:00') AS last_week_monday,
CONCAT(SUBDATE(@THIS_MONDAY, 1), ' 23:59:59') AS last_week_sunday;