Solved

SQL Query to Order By timestamp

Posted on 2012-03-15
3
352 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
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
jogos earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Introduction In the following article I’ll be discussing and demonstrating several different ways of how images can be put on a report. I’m using SQL Server Reporting Services 2008 R2 CTP, more precisely version 10.50.1352.12, but the methods ex…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now