Posted by: jwalin on: February 18, 2009
this is only for SQL 2005
SELECT CategoryID, CategoryName
FROM (SELECT Row_Number() OVER(ORDER BY CategoryName DESC) as RowNum,
CategoryID, CategoryName FROM dbo.Categories) Cat
OR
SELECT ROW_NUMBER() OVER (ORDER BY Field1) As SrNo, Field1, Field2 FROM < Table>
if you are using SQL 2000
By using the % (modulo) operator we get rows with the categoryid is odd. This does not perfectly give you everyother row since we could have a missing value (the row could have been deleted). Even worse, your additional search criteria could select data that is not evenly distributed between even and odd ids.
This will work every time:
DECLARE @tCat TABLE (TID int identity(1,1), CategoryID int,CategoryName varchar(100))
INSERT @tCat (CategoryID, CategoryName )
SELECT CategoryID, CategoryName
FROM dbo.Categories
SELECT CategoryID, CategoryName FROM @tCat
thanx david!!!
Happy Programming !!!!