Tuesday, August 12, 2014

Select data beween two row number of result set (LIMIT IN MSSQL)

In MYSQL, LIMIT  clause is used to select records from the beginning, middle and end of a result set.
The LIMIT clause accepts one or two arguments. The values of both arguments must be zero or positive integer constants.

Select * from Tablename LIMIT offset,count

Here offset specifies the offset of the first row to return and count specifies maximum number of rows to return.

But in MSSQL it can get different way. TOP Clause is used to specify the number of records to return.

SELECT TOP number|percent columnname(s)
FROM tablename


MSSQL SERVER Does not support LIMIT clause. You can implement it in different way. Suppose you want to select rows between 6 to 10, you should try below MSSQL query

SELECT * FROM (
  SELECT *, ROW_NUMBER() OVER (ORDER BY table_column) as row FROM Tablename
 ) a WHERE row > 5 and row <= 10