1

我知道这听起来很荒谬……我知道……但我希望能够选择本月的前 7 天并得到这样的结果。

这就是我所拥有的:

SELECT row_number () OVER (ORDER BY DateD), *
  FROM (SELECT DATENAME (dw, GETDATE ()) AS 'DateName', getdate () AS 'DateD'
        联盟
        选择 DATENAME (dw, GETDATE () + 1) AS 'DateName',
               getdate () + 1 AS 'DateD'
        联盟
        选择 DATENAME (dw, GETDATE () + 2) AS 'DateName',
               getdate () + 2 AS 'DateD'
        联盟
        选择日期名称(dw,GETDATE()+ 3)作为'日期名称',
               getdate () + 3 AS 'DateD'
        联盟
        选择 DATENAME (dw, GETDATE () + 4) AS 'DateName',
               getdate () + 4 AS 'DateD'
        联盟
        选择日期名称(dw,GETDATE()+ 5)作为'日期名称',
               getdate () + 5 AS 'DateD'
        联盟
        选择 DATENAME (dw, GETDATE () + 6) AS 'DateName',
               getdate () + 6 AS 'DateD') 查询表
4

5 回答 5

1

这是一个非常丑陋的查询,可以为您提供结果:

SELECT row_number () OVER (ORDER BY DateD), *
FROM 
(
    SELECT DATENAME (dw, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) AS 'DateName', 
        DATEADD(month, DATEDIFF(month, 0, getdate()), 0) AS 'DateD'
    UNION
    SELECT DATENAME (dw, DateAdd(day, 1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))) AS 'DateName',
           DateAdd(day, 1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) AS 'DateD'
    UNION
    SELECT DATENAME (dw, DateAdd(day, 2, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))) AS 'DateName',
           DateAdd(day, 2, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) AS 'DateD'
    UNION
    SELECT DATENAME (dw, DateAdd(day, 3, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))) AS 'DateName',
           DateAdd(day, 3, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) AS 'DateD'
    UNION
    SELECT DATENAME (dw, DateAdd(day, 4, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))) AS 'DateName',
           DateAdd(day, 4, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) AS 'DateD'
    UNION
    SELECT DATENAME (dw, DateAdd(day, 5, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))) AS 'DateName',
           DateAdd(day, 5, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) AS 'DateD'
    UNION
    SELECT DATENAME (dw, DateAdd(day, 6, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))) AS 'DateName',
           DateAdd(day, 6, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) AS 'DateD'
) queryTable

请参阅带有演示的 SQL Fiddle

于 2013-01-28T16:38:17.033 回答
1

这是获取@n当月天数的更简单方法。如果您需要它用于不同的月份,只需替换GETDATE()为表示您想要的月份内任何日期时间值的变量。

;WITH x AS 
(
  SELECT TOP (@n) n = ROW_NUMBER() OVER (ORDER BY [object_id])
  FROM sys.all_objects
),
y(d,n) AS
(
  SELECT DATEADD(DAY, n-1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)),
   n FROM x
)
SELECT 
  RowNumber = n, 
  [Weekday] = DATENAME(WEEKDAY, d), 
  [Date_In_Ambiguous_Format] = CONVERT(CHAR(10), d, 101),
  [Date_In_Proper_DataType] = d
FROM y;

请参阅此博客系列关于无循环且无重复代码的生成集:

http://www.sqlperformance.com/2013/01/t-sql-queries/generate-a-set-1
http://www.sqlperformance.com/2013/01/t-sql-queries/generate-a -set-2
http://www.sqlperformance.com/2013/01/t-sql-queries/generate-a-set-3

于 2013-01-28T16:38:20.107 回答
0

您可以使用此递归 CTE:

WITH dayscte ( d ) 
     AS (SELECT DATEADD(DAY, 0, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AS d 
         UNION ALL 
         SELECT Dateadd(d, 1, d) 
         FROM   dayscte 
         WHERE  Datepart(day, d) < 7)  -- number of days you want
SELECT d, 
       Datepart(wk, d) AS week_number, 
       Datename(dw, d) AS day_name, 
       Datename(m, d)  AS month_name, 
       Datename(q, d)  AS [quarter] 
FROM   dayscte 
OPTION (maxrecursion 800); 

Demo

于 2013-01-28T16:40:32.627 回答
0
WITH Dates AS
(
    SELECT DATEADD(DAY, - (DATEPART(DAY, GETDATE()) - 1), CONVERT(VARCHAR, GETDATE(), 101)) as DateDay
    UNION ALL
    SELECT DateDay + 1
    FROM Dates
    WHERE DATEPART(DAY, DateDay) < 7
)

SELECT
    DATEPART(DAY, DateDay),
    DATENAME(DW, DateDay),
    DateDay
FROM Dates
于 2013-01-28T16:41:28.383 回答
0

使用 SQL Server,您可以查询 master..spt_values 以获取数字列表(不是完整的数字列表,但绝对是 1 到 7):

SELECT 
   row_number () OVER (ORDER BY Number) as Row, 
   DATENAME (dw,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GetDate())-Number),GetDate()),101) ) AS DtName,
   CONVERT(VARCHAR, CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GetDate())-Number),GetDate()),101), 101) As Dt
FROM (SELECT Number FROM master..spt_values WHERE Number BETWEEN 1 AND 7 AND Type = 'P') t

只是另一种方法——玩得开心!这是小提琴

祝你好运。

于 2013-01-28T16:49:12.133 回答