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
  • 69,480
  • 12
  • 108
  • 116
Geek
  • 3,187
  • 15
  • 70
  • 115
  • 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
  • 88,164
  • 40
  • 182
  • 265
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
  • 72,686
  • 18
  • 132
  • 173
  • 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,922
  • 6
  • 43
  • 70