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 2000 Forums
 SQL Server Development (2000)
 Next and Previous Record of a particular record

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-01-30 : 11:14:23
Mike writes "Hello

I wonder if you can answer this SQL question:

Lets start with an example :

SELECT *
FROM ContactWorks
WHERE (ID= 787)
ORDER BY LastName

This basically gives me the record with ID = 787.

Supposed I also wanted the record right before and after this record. What would be the SQL statement?
Note: All records have to be sorted by LastName before we select the the 3 records (Current, Previous and Next).

So, if the table is :
ID ------ LastName
--------------------------------
216 ------ Alan
6 ------ Zako
787 ------ Baker
8 ------ Cater

The result should be :

216 ------ Alan
787 ------ Baker
8 ------ Cater

-------------------------------

Looking forward to hear from you.

Regards,
Mike Q.
"

scrubz
Starting Member

1 Post

Posted - 2003-02-19 : 13:32:38
This works beautifully for working with a last name only. In my situation, I need to find next and previous records alphabetically, but I also need to take into account first name and middle initial. So, is there an easy modification to this to sort by last name, then first name, and finally middle initial? Thanks.
Go to Top of Page
   

- Advertisement -