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)
 Ordered Information

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-01-28 : 08:42:18
Russel writes "Hi,

These are some questions that I would want a solution for

1) a cretain row in a table ie. if I want the 5th row the 5th row must come up.

2) odd rows in a table

3) even rows in a table

4) row number along with the each record details
eg ....
1 ..........................
2 ..........................
3 ..........................


Kindly address these. I would require a select statement and not a store procedure for the same results"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-01-28 : 09:25:57
If you search SQL Team for "row number" you'll get a bunch of articles, this is the first:

http://www.sqlteam.com/item.asp?ItemID=1491

Also search for "TOP" and check graz's article "What's After Top?".

For odd numbered rows, you can use Modulo (% in SQL Server) to mod the row number by 2:

SELECT * FROM myTable WHERE row_number % 2 = 0 --gets even rows
SELECT * FROM myTable WHERE row_number % 2 = 1 --gets odd rows

Be aware that there is no built-in row number, you have to calculate it. Also, it's arbitrary, and depends entirely on how you ORDER BY your SELECT statement. If you don't have an ORDER BY clause you will not get consistent results.

Go to Top of Page
   

- Advertisement -