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 find missing values in a date sequence using EXCEPT?

Execute the following Microsoft SQL Server T-SQL script in Management Studio Query Editor to find missing OrderDate-s in the SalesOrderHeader table between first (MIN) and last (MAX) dates.

-- SQL missing dates - SQL missing order dates - SQL missing sales dates

-- SQL date gaps - SQL EXCEPT set operator - SQL MIN & MAX functions

USE AdventureWorks;

DECLARE  @MaxOrderDate CHAR(8),

         @MinOrderDate CHAR(8),

         @RunningDate  CHAR(8)

-- SQL Server table variable

DECLARE  @AllDates  TABLE(

                          DATE CHAR(8)

                          )

 

SELECT @MaxOrderDate = convert(CHAR(8),max(OrderDate),112),

       @MinOrderDate = convert(CHAR(8),min(OrderDate),112)

FROM   Sales.SalesOrderHeader

 

SET @RunningDate = @MinOrderDate

 

WHILE (@RunningDate <= @MaxOrderDate)

  BEGIN

    INSERT @AllDates

    SELECT @RunningDate

    

    SET @RunningDate = convert(CHAR(8),dateadd(DAY,1,@RunningDate),112)

  END

 

SELECT MissingDates=DATE

FROM   @AllDates

EXCEPT

SELECT DISTINCT convert(CHAR(8),OrderDate,112)

FROM   Sales.SalesOrderHeader

GO

/* Results

 

MissingDates

20010816

20010911

20011024

*/

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