How to return the last entered item?

I am trying to get the last entry from my database based off the date entered. The sql is not working exactly as I want it. Here is my sql

SELECT TOP 1 * FROM BILLS_HITS ORDER BY DATETIMEADD DESC

Can anyone see anything wrong with it? The sql returns a record with the date - "6/6/2012 7:10:11 AM" and not one that is for 6/10/2012.

Answers


Use a more database-agnostic approach:

SELECT *
FROM
    BILLS_HITS
WHERE
    DATETIMEADD = (SELECT MAX(DATETIMEADD) FROM BILLS_HITS)

The TOP keyword is more specific to MSSQL, while the above query would work in most other DBMSs including MSSQL.

Also, to make the selection fast, you'll likely want to put an index on the column DATETIMEADD

If it doesn't work, make sure the DATETIMEADD column is of a proper date/datetime type and not just a varchar/char/text string.


Need Your Help

960 Grid: the proper way to make grid lists

css 960.gs

When using the 960 grid, what is the proper way to make lists of information? By lists of information, I am specifically referring to a list of data you would show a user on a page, not a listbox or

Catching the close or post to Facebook event of the Facebook comment box

javascript facebook facebook-like facebook-javascript-sdk

The new Facebook like button has a comment box, as you can see on the screenshot below.