Solved

SQL Query to Order By timestamp

Posted on 2012-03-15
3
371 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 500 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 Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

Written by Valentino Vranken. Introduction: The first step of creating a SQL Server Reporting Services (SSRS) report involves setting up a connection to the data source and programming a dataset to retrieve data from that data source.  The data…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 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