Solved

Get Year Month Date in order

Posted on 2015-02-17
7
174 Views
Last Modified: 2015-02-17
How can I order year month date?
Currently, this is the result.

year  moth date
2015      1        2
2015      1       12
2015      1       5
2015      2       16
2015      1       19
2015      1       26
2015      2       2
2015      2       12
2014      4       4
2014      4       7
2014      5       19

I want the result like this.

year  moth date
2015 2  16
2015 2  12
2015 2  2
2015 1  26
etc


declare @trancode varchar(3)

set @trancode = 'aaa'

select   YEAR(TimeStamp) as [year], MONTH(TimeStamp) as [month], DATEPART(day,timestamp) as [Date]
from tabl1
where trancode =@trancode and YEAR(TimeStamp) >= YEAR(GETDATE()) - 2
group by YEAR(TimeStamp), MONTH(TimeStamp), DATEPART(day,timestamp)
order by  YEAR(TimeStamp) desc
0
Comment
Question by:VBdotnet2005
7 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40614660
order by   [month] desc

This is the order in TSQL:
1. SELECT
2. FROM
3. WHERE
4. GROUP BY
5. HAVING
6. ORDER BY

But logical query processing order is:
1. FROM
2. WHERE
3. GROUP BY
4. HAVING
5. SELECT
6. ORDER BY

Meaning the alias [month] from SELECT is available for ORDER BY.

Mike
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 40614668
select convert (cast(year as varchar(4)) + '/' + cast(month as varchar(2)) + '/' + cast(day as varchar(2)), 111) as newDate
order by year desc, order by month desc, order by day desc
0
 
LVL 13

Expert Comment

by:LIONKING
ID: 40614672
Use ORDER BY TimeStamp DESC
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 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40614675
I was typing to fast . . . correct code below:

select convert (cast(year as varchar(4)) + '/' + cast(month as varchar(2)) + '/' + cast(date as varchar(2)), 111) as newDate
order by year desc, month desc, date desc
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40614706
Change the date to an integer and order by that.

I'll assume the top part of your query works

select   YEAR(TimeStamp) as [year], MONTH(TimeStamp) as [month], DATEPART(day,timestamp) as [Date]
from tabl1
where trancode =@trancode and YEAR(TimeStamp) >= YEAR(GETDATE()) - 2

Open in new window


Add this
order by cast(cast(year(timestamp) as nvarchar(4)) + right('0' + cast(month(timestamp) as nvarchar(2)), 2) + right('0' + cast(day(timestamp) as nvarchar(2)), 2) as int) desc

Open in new window

0
 

Author Closing Comment

by:VBdotnet2005
ID: 40614735
thank you
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40615264
Just as a quick observation, as you are using SQL terms as column names, so you really should protect from possible misinterpretation by using [] or " "

order by [year] desc, [month] desc, [date] desc

or, don't use SQL terms as column names :)
0

Featured Post

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!

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

757 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

23 Experts available now in Live!

Get 1:1 Help Now