Limiting the number of queries returns in SQL Server 2008

This is my query

SELECT Fullname, rank, id_no, TIN, birthdate, 
hair, eyes, Blood, height, weight, marks, name, address
FROM  [******_DOMAIN\****_*****].*******view

Problem is, source table has too many duplicates, how do I my limit query to the latest row on the database?

I'm using SQL Server 2008.

Thanks In advance

My next problem is that the view shows me a birthdate string format of yyyymmdd, I need to change it to mm/dd/yyyy can please provide me a function? using the same string above?

Upvotes: 0

Views: 134

Answers (3)

Walter
Walter

Reputation: 369

Use TOP for limiting the records and ORDER BY to sort the records based on your desired column.

example.

SELECT    TOP 5 Fullname, rank, id_no, 
          TIN, birthdate, hair, eyes, 
          Blood, height, weight, marks, name, address 
FROM      viewName
ORDER BY  yourDesiredClumn desc

this will display only 5 records.

Upvotes: 2

Shehzad Bilal
Shehzad Bilal

Reputation: 2523

Use this:

Select TOP(#) Fullname, rank, id_no, TIN, birthdate, hair, eyes, ....

Upvotes: 1

David B
David B

Reputation: 2698

For duplicates, you can limit the records by using SELECT DISTINCT, and to only retrieve a certain amount of records, you can use SELECT TOP # where # is the amount of records. As for latest record- I'm not sure it can be done unless you have a date field on the record of when it was inserted.

Upvotes: 2

Related Questions