Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fix Date gap

Status
Not open for further replies.

CTl06

Programmer
Aug 22, 2006
17
US
Hello all,
I am trying to fix data in the database that incorrect record start date and sometime having incorrect record enddate. Can you please help me what is the best way to fix this data in SQL. Here are examples of problem data.

Scenario #1:
Recordid SartDate EndDate
1 01/01/2010 01/02/2010
2 01/02/2010 01/05/2010--correct date is start date of recorid 3
3 01/03/2010 01/04/2010
4 01/04/2010

Scenario #2:
Recordid SartDate EndDate
1 01/01/2010 01/02/2010
2 01/02/2010 01/05/2010
3 01/02/010 01/06/2010-- Correct start dt is recordid 2 enddate
4 01/062010

 
How do you know which date is incorrect (start or end)?
And also is there ALWAYS record ID is in sequence?

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
bborissov,

Most of the time record enddate is incorrect and yes there is always a recordid in sequence.
 
MOST of the time didn't work :)
It should be ALL the time or nothing :)
Code:
SELECT YourTable.*,
       Tbl1.StartDate AS MaybeCorrectDate
FROM YourTable
INNER JOIN YourTable Tbl1 
      ON (YourTable.RecordId+1) = Tbl1.RecordId
See if this is what you need?

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Borissov,

Sorry, I did not mean to say that the RecordId is always requence.. The record id is identity seed but not all the group of records with bad date happen in the sequence.. for example:

1 01/01/2010 01/02/2010
100 01/02/2010 01/05/2010--correct date is start date of recorid 3
135 01/03/2010 01/04/20104 01/04/2010
 
OK, what makes records in group and what version of SQL Server you use?

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
I use sql 2005 and identifer that make records in group is other field

Recordid SartDate EndDate Id
1 01/01/2010 01/02/2010 10
2 01/02/2010 01/05/2010 10
3 01/02/010 01/06/2010 10 -- Correct start dt is recordid 2 enddate
4 01/062010 10
 
Code:
SELECT YourTable.*,
       Tbl1.StartDate AS MaybeCorrectDate
FROM (SELECT *, 
             ROW_NUMBER() OVER (PARTITION BY Id ORDER BY StartDate) AS RowN
             FROM YourTable) YourTable
INNER JOIN (SELECT *, 
             ROW_NUMBER() OVER (PARTITION BY Id ORDER BY StartDate) AS RowN
             FROM YourTable) Tbl1
      ON (YourTable.RowN+1) = Tbl1.RowN
NOT TESTED!

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top