我知道这听起来很荒谬......我知道......但我希望能够选择一个月的前7天并得到这样的结果。
这就是我所拥有的:
SELECT row_number () OVER (ORDER BY DateD), * FROM (SELECT DATENAME (dw, GETDATE ()) AS 'DateName', getdate () AS 'DateD' UNION SELECT DATENAME (dw, GETDATE () + 1) AS 'DateName', getdate () + 1 AS 'DateD' UNION SELECT DATENAME (dw, GETDATE () + 2) AS 'DateName', getdate () + 2 AS 'DateD' UNION SELECT DATENAME (dw, GETDATE () + 3) AS 'DateName', getdate () + 3 AS 'DateD' UNION SELECT DATENAME (dw, GETDATE () + 4) AS 'DateName', getdate () + 4 AS 'DateD' UNION SELECT DATENAME (dw, GETDATE () + 5) AS 'DateName', getdate () + 5 AS 'DateD' UNION SELECT DATENAME (dw, GETDATE () + 6) AS 'DateName', getdate () + 6 AS 'DateD') queryTable
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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);
答案 3 :(得分: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
答案 4 :(得分: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
另一种方法 - 玩得开心!这是Fiddle。
祝你好运。