Solved

How to order by date ASCENDING for previous years

Posted on 2013-01-08
8
228 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
  • 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 142

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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now