Solved

How to order by date ASCENDING for previous years

Posted on 2013-01-08
8
234 Views
Last Modified: 2013-01-08
SELECT distinct
  Convert ( char ( 10), Resolved_Date, 110 ) 'one',
  Convert ( char ( 10), Resolved_Date, 105 ) 'two',
  Convert ( char ( 10), Resolved_Date, 120 ) 'three'
From
  Receiving_log
where Resolved_Date > '01/01/2012'
Order By 
  'one' asc

Open in new window


results in:

01-02-2013      02-01-2013      2013-01-02
01-03-2012      03-01-2012      2012-01-03
01-03-2013      03-01-2013      2013-01-03
01-04-2012      04-01-2012      2012-01-04
01-04-2013      04-01-2013      2013-01-04

i would like to have it like this:

01-04-2013      04-01-2013      2013-01-04
01-03-2013      03-01-2013      2013-01-03
01-02-2013      02-01-2013      2013-01-02
01-03-2012      03-01-2012      2012-01-03
01-04-2012      04-01-2012      2012-01-04
0
Comment
Question by:JessyRobinson1234
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38753941
Instead of ORDER BY 'one' ASC, do ORDER BY ResolvedDate ASC. One is a char field you created, so the order is based on the output as a string, not as a date. Use the date field itself for the correct order.
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38753982
SELECT distinct
  Convert ( char ( 10), Resolved_Date, 110 ) 'one',
  Convert ( char ( 10), Resolved_Date, 105 ) 'two',
  Convert ( char ( 10), Resolved_Date, 120 ) 'three'
From
  Receiving_log
where Resolved_Date > '01/01/2012'
Order By
 Resolved_Date asc
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38754148
you could order by "three" .... as that format will sort correctly.

please read up this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:JessyRobinson1234
ID: 38754152
Ok but then I have multiple identical as here were many items created in the same day with a different time stamp. The distinct no longer works as desired. How do I get around that?
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38754160
You can use a format that will sort by date even as a string, like 112 or 120.
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38754263
Try this




SELECT
  Convert ( char ( 10), X.Resolved_Date, 110 ) 'one',
  Convert ( char ( 10), X.Resolved_Date, 105 ) 'two',
  Convert ( char ( 10), X.Resolved_Date, 120 ) 'three'

From

( Select Resolved_Date
From
  Receiving_log
where Resolved_Date > '01/01/2012'
 ) X

Order By
 X.Resolved_Date asc
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38754285
If you use ORDER BY three as angelIII proposed, it should work for you. Don't use it between '' though, or sql will order by the string 'three' rather than by the field three.
0
 

Author Closing Comment

by:JessyRobinson1234
ID: 38754505
Thank you, works like a charm
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

687 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