[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

SQL Query to Order By timestamp

Hi,

I have the following query (See attachment) to select data from my archive database, join different archives on the unique column 'TLInstance', and grab the names for each of the archives.

The trouble I'm having is that the returned data is not ordered by the Date held in the Timestamp column. For example, if I ask for data between 24-2-2012 to 9-3-2012, the March data is returned at the top of the report, because the Day of Month is a lower number. So it is ordering by Day of Month, instead of the date, meaning March data is returned before February Data.

I need to modify this query to order the returned data by Timestamp as a true Date, so the lowest Date is always returned first, regardless of the Day of Month.

Cna anyone assist?
0
wint100
Asked:
wint100
  • 2
1 Solution
 
jogosCommented:
Don't see attachement.

But when things get ordered on the day-part and not as a date that means that the order by is not aware that it is a datetime. It is ordering it as a varchar.

Convert (datetime, yourdatecol, 105)

Why the 105? That's the date-style that matches your '24-2-2012'
see http://msdn.microsoft.com/en-us/library/ms187928.aspx
0
 
wint100Author Commented:
I guess I should have listed to Mark wills when we set this thing up originally:

http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_26855116.html

It all makes sense now..
0
 
jogosCommented:
Now you will remember better!  
Basic rule keep something as long in it's natural datatype as possible and do explicit conversions where you change of datatype.(or compare 2 different types) and don't do a conversion xin a where-clause But you could add creating a backup of the database before altering it, to be able to restore it in case of errors. that would be used in an index.  

The gain : performance and predictible behaviour
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now