填充一个临时表的日期的最简单的方法,并包括2个日期参数

问题描述 投票:21回答:8

什么是填充一个临时表的日期,包括2个日期参数之间的最简单的方法。我只需要一个月中日期的第一天。

因此,举例来说,如果@StartDate = '2011-01-01' 和@EndDate = '2011-08-01'

然后我想这回在表

2011-01-01
2011-02-01
2011-03-01
2011-04-01
2011-05-01
2011-06-01
2011-07-01
2011-08-01
sql sql-server-2005 tsql
8个回答
24
投票

这个工程即使@StartDate并非当月的第一天。我假设,如果不是这个月的开始,你要开始与第一下个月。否则删除+1:

;WITH cte AS (
SELECT CASE WHEN DATEPART(Day,@StartDate) = 1 THEN @StartDate 
            ELSE DATEADD(Month,DATEDIFF(Month,0,@StartDate)+1,0) END AS myDate
UNION ALL
SELECT DATEADD(Month,1,myDate)
FROM cte
WHERE DATEADD(Month,1,myDate) <=  @EndDate
)
SELECT myDate
FROM cte
OPTION (MAXRECURSION 0)

16
投票
declare @StartDate date = '2014-01-01';
declare @EndDate date = '2014-05-05';

;WITH cte AS (
    SELECT @StartDate AS myDate
    UNION ALL
    SELECT DATEADD(day,1,myDate) as myDate
    FROM cte
    WHERE DATEADD(day,1,myDate) <=  @EndDate
)
SELECT myDate
FROM cte
OPTION (MAXRECURSION 0)

7
投票
declare @StartDate datetime
declare @EndDate datetime
select @StartDate = '2011-01-01' ,  @EndDate = '2011-08-01'

select @StartDate= @StartDate-(DATEPART(DD,@StartDate)-1)

declare @temp  table
(
TheDate datetime
)
while (@StartDate<=@EndDate)
begin
insert into @temp
values (@StartDate )
select @StartDate=DATEADD(MM,1,@StartDate)
end
select * from @temp

通过返回到起始日期的当月的第一天始工作,即使@StartDate不是该月的第一天


6
投票

这是在2008年SQL R2测试

Declare @StartDate datetime = '2015-03-01'
Declare @EndDate datetime = '2015-03-31'
declare @temp Table
(
DayDate datetime
);

WHILE @StartDate <= @EndDate
begin
 INSERT INTO @temp (DayDate) VALUES (@StartDate);
 SET @StartDate = Dateadd(Day,1, @StartDate);
end ;

select * from @temp

结果:

DayDate
-----------------------
2015-03-01 00:00:00.000
2015-03-02 00:00:00.000
2015-03-03 00:00:00.000
2015-03-04 00:00:00.000
...

1
投票

解:

DECLARE  @StartDate DATETIME
        ,@EndDate DATETIME;

SELECT   @StartDate = '20110105'
        ,@EndDate = '20110815';

SELECT  DATEADD(MONTH, DATEDIFF(MONTH, 0, DATEADD(MONTH, v.number, @StartDate)), 0) AS FirstDay
--or Andriy M suggestion:
--SELECT    DATEADD(MONTH, DATEDIFF(MONTH, 0, @StartDate) + v.number, 0) AS FirstDay
INTO    #Results
FROM    master.dbo.spt_values v
WHERE   v.type = 'P'        
AND     DATEDIFF(MONTH, @StartDate, @EndDate) >= v.number;

SELECT  *
FROM    #Results;

DROP TABLE #Results;

结果:

FirstDay
-----------------------
2011-01-01 00:00:00.000
2011-02-01 00:00:00.000
2011-03-01 00:00:00.000
2011-04-01 00:00:00.000
2011-05-01 00:00:00.000
2011-06-01 00:00:00.000
2011-07-01 00:00:00.000
2011-08-01 00:00:00.000

1
投票

有趣的是,这是更快地从列举的数据按照this article创建。

DECLARE @StartDate DATE = '10001201';
DECLARE @EndDate DATE   = '20000101';

DECLARE @dim TABLE ([date] DATE)

INSERT @dim([date])
SELECT d
FROM
(
  SELECT
      d = DATEADD(DAY, rn - 1, @StartDate)
  FROM 
  (
      SELECT TOP (DATEDIFF(DAY, @StartDate, @EndDate)) 
          rn = ROW_NUMBER() OVER (ORDER BY s1.[object_id])
      FROM
          sys.all_objects AS s1
      CROSS JOIN
          sys.all_objects AS s2
      ORDER BY
          s1.[object_id]
  ) AS x
) AS y;

在我的机器,它的周围有大日期范围快60%。递归方法可以填充2000年有价值的数据在3秒左右,虽然,看起来要好很多,所以我真的不建议使用这种方法只是为了增加天。


1
投票

修正空日期:

IF OBJECT_ID('tempdb..#dim') IS NOT NULL 

    DROP TABLE #dim

CREATE  TABLE #dim ([date] DATE)

if not @Begin_Date is null and not @End_Date is null

begin
    INSERT #dim([date])
    SELECT d
    FROM(

      SELECT
          d = DATEADD(DAY, rn - 1, @Begin_Date)
      FROM 
      (
          SELECT TOP (DATEDIFF(DAY, @Begin_Date, @End_Date)) 
              rn = ROW_NUMBER() OVER (ORDER BY s1.[object_id])
          FROM
              sys.all_objects AS s1
          CROSS JOIN
              sys.all_objects AS s2
          ORDER BY
              s1.[object_id]
      ) AS x
    ) AS y;
end

0
投票
CREATE TABLE #t (d DATE)

INSERT INTO #t SELECT GETDATE()

GO

INSERT #t SELECT DATEADD(DAY, -1, MIN(d)) FROM #t

GO 10
© www.soinside.com 2019 - 2024. All rights reserved.