如何在不创建存储过程,日历表或递归函数的情况下列出SQL Server中两个日期参数之间的所有日期?
答案 0 :(得分:2)
始终是递归CTE选项:
DECLARE @STARTDATE DATETIME
DECLARE @ENDDATE DATETIME
SET @STARTDATE = '2015-01-01'
SET @ENDDATE = '2015-12-31'
;WITH DATE_RANGE (DATES) AS (
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)
UNION ALL SELECT DATEADD(DAY, 1, DATES)
FROM DATE_RANGE
WHERE DATEADD(DAY, 1, DATES) <= @ENDDATE)
SELECT DATES
FROM DATE_RANGE
OPTION (MAXRECURSION 0)
请务必使用MAXRECURSION
选项,否则您的结果将被限制为默认值为100。
答案 1 :(得分:0)
这在Master数据库的spt_values表上使用Row_Number
来创建日期范围内的年,月和日期列表。
然后将其内置到日期时间字段中,并过滤为仅返回输入的日期参数中的日期。
执行非常快,并在不到1秒的时间内返回500年的日期(182987天)。
Declare @DateFrom datetime = '2000-01-01'
declare @DateTo datetime = '2500-12-31'
Select
*
From
(Select
CAST(CAST(years.Year AS varchar) + '-' + CAST(Months.Month AS varchar) + '-' + CAST(Days.Day AS varchar) AS DATETIME) as Date
From
(select row_number() over(order by number) as Year from master.dbo.spt_values) as Years
join (select row_number() over(order by number) as Month from master.dbo.spt_values) as Months on 1 = 1
join (select row_number() over(order by number) as Day from master.dbo.spt_values) as Days on 1 = 1
Where
Years.Year between datepart(year,@DateFrom) and datepart(year,@DateTo)
and Months.Month between 1 and 12
and
Days.Day between 1 and datepart(day,dateadd(day,-1,dateadd(month,1,(CAST(CAST(Years.Year AS varchar)+'-' + CAST(Months.Month AS varchar) + '-01' AS DATETIME)))))
) as Dates
Where Dates.Date between @DateFrom and @DateTo
order by 1
答案 2 :(得分:0)
以下将是您的解决方案
import com.datastax.driver.core.Cluster
import com.datastax.driver.core.ProtocolOptions.Compression
val builder: Cluster.Builder = Cluster.builder
builder.addContactPoint(A.address)
builder.addContactPoint(B.address)
builder.addContactPoint(C.address)
val cluster: Cluster = builder.withCompression(Compression.SNAPPY).build
答案 3 :(得分:0)
简单的结果。
DECLARE @DATE1 DATE
DECLARE @DATE2 DATE
SET @DATE1 ='20020101'
SET @DATE2 = '20020311'
WHILE(@DATE1<=@DATE2)
Begin
PRINT @DATE1
set @DATE1 = DATEADD(dd,1,@DATE1)
END