how can we select the first record in a given set of rows?

Posted by Udaysimha under Sql Server on 1/18/2012 | Points: 10 | Views : 2018 | Status : [Member] | Replies : 2
Hi All,
Please any body let me know how can we select the first record in a given set of rows.
Thanks

uday


Responses

Posted by: Blessyjees on: 1/18/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,

You can try top 1 for getting first record
For eg:
select top 1 * from tblEmployee

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Udaysimha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Niladri.biswas on: 2/8/2012 [Member] Platinum | Points: 25

Up
0
Down
You can use TOP or Row_Number function for this

Declare @t table(Emp_Id int identity, Emp_name varchar(20), Gender varchar(1)) 

Insert into @t
Select 'sanjay','M' union all Select 'julie','F' union all
Select 'santwana','M' union all Select 'ss ss','M' union all
Select 'sanjay345','M'

Select x.* From(
Select Rn = ROW_NUMBER() Over(Order by(select 1)),*
From @t)x
where x.Rn =1


OR

Select Top 1 * From @t


Result
-------

Rn	Emp_Id	Emp_name	Gender

1 1 sanjay M


Best Regards,
Niladri Biswas

Udaysimha, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response