在SQL中获取本周的每一天

时间:2017-06-26 17:39:06

标签: sql sql-server tsql

我正在尝试编写一个sql查询来获取一周中的每一天。 就像我运行SELECT GETDATE()我今天得到的日期如果运行getdate() - 1我只得到昨天日期的列。相反,我希望在不同的列中获得一周中的每一天,有人请帮助SQL。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) Monday,
       DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 1 Tuesday,
       DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 2 Wednsday,
       DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 3 Thursday,
       DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 4 Friday,
       DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 5 Saturday,
       DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 6 Sunday;

enter image description here

答案 1 :(得分:0)

这有用吗?

SELECT 
  Day1 = CAST(getdate() AS date),
  Day2 = CAST(getdate()+1 AS date),
  Day3 = CAST(getdate()+2 AS date),
  Day4 = CAST(getdate()+3 AS date),
  Day5 = CAST(getdate()+4 AS date),
  Day6 = CAST(getdate()+5 AS date),
  Day7 = CAST(getdate()+6 AS date);

返回:

Day1       Day2       Day3       Day4       Day5       Day6       Day7
---------- ---------- ---------- ---------- ---------- ---------- ----------
2017-06-26 2017-06-27 2017-06-28 2017-06-29 2017-06-30 2017-07-01 2017-07-02

如果这不是您要找的内容,您可以发布您希望查询返回的内容吗?