1

I have a table with 1000 records. One column is the publish date which takes the format '2008-01-02 00:00:00.000'. I want to query the SQL DB to get the record with the latest publish date. should i just do a compare or there is some other filter?

Lamak
  • 67,466
  • 11
  • 101
  • 112
Geek
  • 3,137
  • 14
  • 60
  • 104
  • So, is `2008-01-02 00:00:00.000` type `DATETIME` or a `CHAR`?. If its not `DATETIME`, it represents January second or February first? – Lamak Apr 17 '12 at 18:57

3 Answers3

4
SELECT * FROM tbl WHERE publishdate = (SELECT MAX(publishdate) FROM tbl)
Cade Roux
  • 85,870
  • 40
  • 177
  • 264
3

If you want just one record:

SELECT TOP 1 * FROM mytable ORDER BY publishdate DESC

If you want ALL books with the highest publish date, use Cade Roux's query.

David
  • 70,778
  • 16
  • 128
  • 169
  • how do i convert this publish date to 'mm/dd/yy' in SQL? – Geek Apr 17 '12 at 19:21
  • 1
    See this link on formatting dates, but bear in mind that after you format it, the returned date will actually be a text (varchar) datatype, not a Date datatype. A date is what it is, and you can't change how the server stores it, you can only use formatting to tell SQL Server to display it in a certain way. http://stackoverflow.com/questions/759292/to-change-date-format-in-sql – David Apr 17 '12 at 19:23
0

If publishdate is datetime

SELECT TOP 1  *
FROM tbl
ORDER BY publishdate DESC
YvesR
  • 5,494
  • 6
  • 43
  • 67