Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Date Update

Author  Topic 

sanjay5219
Posting Yak Master

240 Posts

Posted - 2014-07-03 : 13:10:58
Hi All,
I have one table with 100000 records and now i have to update one column in that. Below is the scenario

I have to update Last_First_Date column based on RowNo. So that previous row first date will be the next row last date

RowNo First_Date Last_First_Date
1 7/2/14 7:07 AM
2 7/2/14 7:03 AM 7/2/14 7:07 AM
3 7/2/14 7:09 AM 7/2/14 7:03 AM
4 7/2/14 7:24 AM 7/2/14 7:09 AM
5 7/2/14 7:34 AM 7/2/14 7:24 AM

Please suggest

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-07-03 : 13:18:08
[code]UPDATE tgt
SET tgt.Last_First_Date = (SELECT TOP(1) src.First_Date FROM dbo.Table1 AS src WHERE src.RowNo < tgt.RowNo ORDER BY src.RowNo DESC)
FROM dbo.Table1 AS tgt[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

sanjay5219
Posting Yak Master

240 Posts

Posted - 2014-07-05 : 11:52:45
Awesome SwePeso
Go to Top of Page
   

- Advertisement -