DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
 
 
SITE SEARCH SQLUSA.com VIDEOS
 
 
SQL E/BOOKS   WORLD, USA & SQL NEWS   FORMAT
SCRIPTS SQL 2005 SQL 2008 ARTICLES
How to generate date series with table-valued function?

Execute the following SQL Server T-SQL script in SSMS Query Editor to create a table-valued function to generate date series between two dates:

USE AdventureWorks2008;

GO

 

CREATE FUNCTION fnDateSeries

               (@StartDate DATETIME,

                @EndDate   DATETIME)

RETURNS @Series TABLE([Day] DATETIME)

AS

  BEGIN

    DECLARE  @iDate DATETIME

    SET @iDate = @StartDate

    

    WHILE (datediff(DAY,@iDate,@EndDate) >= 0)

      BEGIN

        INSERT @Series

        SELECT @iDate

        

        SET @iDate = dateadd(DAY,1,@iDate)

      END

    

    RETURN

  END

 

GO

 

SELECT *

FROM   dbo.fnDateSeries('2008-01-01','2008-01-31')

GO

/*

Day

2008-01-01 00:00:00.000

2008-01-02 00:00:00.000

2008-01-03 00:00:00.000

2008-01-04 00:00:00.000

2008-01-05 00:00:00.000

2008-01-06 00:00:00.000

2008-01-07 00:00:00.000

*/

 

Exam Prep 70-461
SQL 2016 DESIGN & PROGRAMMING
 
 
 
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE