我正试图从太阳报到上周的约会,我的代码是,
Declare
@now DateTime, @fmWeek DateTime, @toWeek DateTime
Set @now = GetDate()
Set @fmWeek = Convert(DateTime, DateAdd(wk, DateDiff(wk, 0, @now), -1))
Set @toWeek = DATEADD(ms,-3,Convert(DateTime, DateAdd(wk, DateDiff(wk, 0, @now), 6)))
select @fmWeek, @toWeek
它显示了从2018-04-15 00:00:00.000
到2018-04-21 23:59:59.997
我需要从前一周的太阳报到不同一周的日期
答案 0 :(得分:2)
我需要将上周太阳的日期安排在不同的一周
我只想减去7天:
Declare @now DateTime = GetDate()
,@fmWeek DateTime, @toWeek DateTime;
Set @fmWeek = DateAdd(wk, DateDiff(wk, 0, @now), -1) - 7;
Set @toWeek = DateAdd(wk, DateDiff(wk, 0, @now), 5) - 7;
select @fmWeek, @toWeek;
<强> DBFiddle Demo 强>
答案 1 :(得分:0)
如果您只想显示两个日期,@ lad2025会给您正确的查询以获取日期。
如果您想要显示这两天的所有日期,您可以尝试以下内容。
Declare @now DateTime = GetDate()
,@fmWeek DateTime, @toWeek DateTime;
Set @fmWeek = DateAdd(wk, DateDiff(wk, 0, @now), -1) - 7;
Set @toWeek = DateAdd(wk, DateDiff(wk, 0, @now), 5) - 7;
SELECT @fmWeek + RN AS DATE FROM
(
SELECT (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)))-1 RN
FROM master..[spt_values] T1
) T
WHERE RN <= DATEDIFF(DAY,@fmWeek,@toWeek)
输出:
Date
------------------------
2018-04-08 00:00:00.000
2018-04-09 00:00:00.000
2018-04-10 00:00:00.000
2018-04-11 00:00:00.000
2018-04-12 00:00:00.000
2018-04-13 00:00:00.000
2018-04-14 00:00:00.000