?
Solved

SQL Query to Order By timestamp

Posted on 2012-03-15
3
Medium Priority
?
373 Views
Last Modified: 2012-03-15
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
Comment
Question by:wint100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
jogos earned 2000 total points
ID: 37723810
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
 
LVL 1

Author Comment

by:wint100
ID: 37723842
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
 
LVL 25

Expert Comment

by:jogos
ID: 37724016
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question