SQL query to select Top Nth record in SQL Server

Last Reply one year ago By Andrea

Posted one year ago

 hi friends am creating a project , 

now i want to bind next nearest record in the table , i am done but i cant able to get exact output. folloeing is my using query.

select TOP 1 * from tbl_FreeAnswer where qid<>'14' and qid>'14' and rollno='R01'

 above query give the result of 2nd record , that mean it takes number 14 as 2 so it give 3rd one result. but i want 15 th record only .

 string query1 = "select TOP 1 * from tbl_FreeAnswer where qid<>'" + lblQuestionId.Trim() + "' and qid>'" + lblQuestionId.Trim() + "' and rollno='"+lblRollNo.Text.Trim()+"' and status='A'  order by id asc";
            SqlCommand nextcmd1 = new SqlCommand(query1, con);
            SqlDataAdapter nextsda1 = new SqlDataAdapter(nextcmd1);
            DataTable dt1 = new DataTable();
            nextcmd1.CommandType = CommandType.Text;
            nextsda1.Fill(dt1);

so friends if anyone suggest me how can i get nearby record of 13 trh number . 

 

thanking you 

Paul.S

You are viewing reply posted by: Andrea one year ago.
Posted one year ago
Hi @paulrajmca,
Please try the following

Select Top Nth Row / Record in SQL Server

It might help you.

Cheers Andrea.