Tuesday, October 29, 2013

MSSQL select specific range of row


SQL2012
SELECT field
FROM table
ORDER BY field ASC
OFFSET 26 ROWS
FETCH NEXT 15 ROWS ONLY


SQL 2005  and above
SELECT field
FROM (SELECT ROW_NUMBER() OVER (ORDER BY table.field ASC) AS field_alias,
field FROM table) table_alias
WHERE table_alias.field_alias >= 10 and table_alias.field_alias <=30;


SQL 2000

SELECT TOP 25 *
FROM (
  SELECT TOP 75 *
  FROM   table
  ORDER BY field ASC
) table_alias
ORDER BY table_alias.field DESC;