Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All

I have a bunch of dates in a column in an SQL Server DB that looks like this
VB
2014-01-29
2014-01-30
2014-02-03
2014-02-04
2014-02-05
2014-02-06
2014-02-11
2014-02-12
2014-02-13
2014-02-17
2014-02-18
2014-02-19
2014-02-20
2014-02-25
2014-02-26
2014-02-27
2014-03-03
2014-03-04
2014-03-05
2014-03-06
2014-03-11
2014-03-12
2014-03-13


There's a couple of questions that I have with SQL.
1. I'm trying to figure out how I can find 2014-03-11 if max day is 2014-03-13? Basically I'm trying to find the first day after there's been a break in dates.
2. Next I'm trying to do a count of the days that are NOT shown in this list
3. Lastly, is there a way to find a certain date in missing days. Example from '2014-03-13' find the 6th missing day that is not shown. (It should '2014-03-01')

I've got more questions but I'm pretty sure I'll figure it out from this.

Thanks

Mike
Posted

This is a quite intriguing classic sql problem called "islands and gaps" (google it) with a whole load of solutions that keep growing.
I'll point you to one specific and good article[^] that should get you started. And you can come back here when you have more specific problems
 
Share this answer
 
Comments
CHill60 23-Mar-14 6:40am    
Nice link. 5'd
Jörgen Andersson 23-Mar-14 15:24pm    
Thanks
This is not a complete answer to all your questions, because it is much simpler to do these in a general purpose language. Do you really need to answer in SQL-only?

Let's suppose so.
There are methods to generate a date range. So you can match the range with the dates you have to find the gaps. See: http://consultingblogs.emc.com/jamiethomson/archive/2007/01/11/T_2D00_SQL_3A00_-Generate-a-list-of-dates.aspx[^].
 
Share this answer
 
Not sure what you want to achive, but have a look at example:

SQL
DECLARE @tmp TABLE (MyDate DATETIME)

INSERT INTO @tmp (MyDate)
VALUES ('2014-01-29'),
('2014-01-30'), ('2014-02-03'),
('2014-02-04'), ('2014-02-05'),
('2014-02-06'), ('2014-02-11'),
('2014-02-12'), ('2014-02-13'),
('2014-02-17'), ('2014-02-18'),
('2014-02-19'), ('2014-02-20'),
('2014-02-25'), ('2014-02-26'),
('2014-02-27'), ('2014-03-03'),
('2014-03-04'), ('2014-03-05'),
('2014-03-06'), ('2014-03-11'),
('2014-03-12'), ('2014-03-13')

SELECT t1.MyDate AS FirstDate, t2.MyDate AS SecondDate, DATEDIFF(d, t1.MyDate, t2.MyDate) AS DayDiff
FROM (
    SELECT ROW_NUMBER() OVER(ORDER BY MyDate) AS RowNo, MyDate
    FROM @tmp
    )
    AS t1
    INNER JOIN
    (
    SELECT ROW_NUMBER() OVER(ORDER BY MyDate) AS RowNo, MyDate
    FROM @tmp
    ) AS t2
    ON t1.RowNo = t2.RowNo-1


Result:
FirstDate               SecondDate              DayDiff
2014-01-29 00:00:00.000 2014-01-30 00:00:00.000 1
2014-01-30 00:00:00.000 2014-02-03 00:00:00.000 4
2014-02-03 00:00:00.000 2014-02-04 00:00:00.000 1
2014-02-04 00:00:00.000 2014-02-05 00:00:00.000 1
2014-02-05 00:00:00.000 2014-02-06 00:00:00.000 1
2014-02-06 00:00:00.000 2014-02-11 00:00:00.000 5
2014-02-11 00:00:00.000 2014-02-12 00:00:00.000 1
2014-02-12 00:00:00.000 2014-02-13 00:00:00.000 1
2014-02-13 00:00:00.000 2014-02-17 00:00:00.000 4
2014-02-17 00:00:00.000 2014-02-18 00:00:00.000 1
2014-02-18 00:00:00.000 2014-02-19 00:00:00.000 1
2014-02-19 00:00:00.000 2014-02-20 00:00:00.000 1
2014-02-20 00:00:00.000 2014-02-25 00:00:00.000 5
2014-02-25 00:00:00.000 2014-02-26 00:00:00.000 1
2014-02-26 00:00:00.000 2014-02-27 00:00:00.000 1
2014-02-27 00:00:00.000 2014-03-03 00:00:00.000 4
2014-03-03 00:00:00.000 2014-03-04 00:00:00.000 1
2014-03-04 00:00:00.000 2014-03-05 00:00:00.000 1
2014-03-05 00:00:00.000 2014-03-06 00:00:00.000 1
2014-03-06 00:00:00.000 2014-03-11 00:00:00.000 5
2014-03-11 00:00:00.000 2014-03-12 00:00:00.000 1
2014-03-12 00:00:00.000 2014-03-13 00:00:00.000 1


As you can see, you can compare dates by using Join. For further information, please see: Visual Representation of SQL Joins[^].
Using WHERE condition[^], you're able to filter your data to show dates where the distance between them is larger than 1 day:
SQL
WHERE DATEDIFF(d, t1.MyDate, t2.MyDate)>1


Try!
 
Share this answer
 
Comments
mzrax 24-Mar-14 20:57pm    
Thanks, I knew it was something like this but just wasn't able to figure out the part like what you did with ON t1.RowNo = t2.RowNo-1.

Thanks

M
Maciej Los 25-Mar-14 2:57am    
You're very welcome ;)
 
Share this answer
 
v2
Comments
mzrax 24-Mar-14 20:55pm    
Thanks for the links they definitely have answered other questions that I had. I knew there was a page like the Microsoft link that you put but I could never find it.

Thanks

M
baliram bhande 25-Mar-14 6:03am    
accept my ans if help my answer to u
baliram bhande 25-Mar-14 6:04am    
Why down voted to me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900