im trying to figure out how to write a sql query for this:
I have a database which contains a column X. X has datetime values if set by a function otherwise it has a default value which looks something like this 0001-01-01.00.00.00.000000
What I am interested in doing is writing sql that will retrieve all the rows of X sorted by latest datetime values.
I thought this would be the answer
Select * from Some_Table st where st.Dbname = "blah" order_by st.x desc
but then I was thinking what happens to the default values? how do they affect the sorting
Any ideas if this is the way to go?
Ordering by date desc will work fine. Why not make the column nullable so you don’t need a default? Also it sounds like you only want the latest single record so you may want to consider using SELECT TOP 1 * FROM blah ORDER BY date DESC