[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL Query to Order By timestamp

Posted on 2012-03-15
3
Medium Priority
?
382 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

656 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