views:

35

answers:

1

Hi, I want to return the date and ID for the latest added record in on of our tables. can anyone suggest right query for that plz. We are using sqlServer

    SELECT [BGArx_ID], [BGArx_PUBLISHED_DATE]      
    FROM TECH_ARTICLES


    WHERE [BGArx_PUBLISHED_DATE] = ???
+4  A: 

Use the ORDER BY clause to sort by the newest record and then limit the query to return just one result.

SELECT BGArx_ID, BGArx_PUBLISHED_DATE 
FROM TECH_ARTICLES 
ORDER BY BGArx_PUBLISHED_DATE DESC LIMIT 1;

EDIT (marc_s)
for SQL Server, which doesn't know the LIMIT keyword, you'd need to use TOP 1 in the select instead:

SELECT TOP 1 BGArx_ID, BGArx_PUBLISHED_DATE 
FROM TECH_ARTICLES 
ORDER BY BGArx_PUBLISHED_DATE DESC
Brad
You need to use "SELECT TOP 1 ......" instead
marc_s
Thanks for the clarification.
Brad