Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 order by or group by

Author  Topic 

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-02-18 : 13:57:58
How can I get these dates to appear the highest to the lowest.
01/29/10
02/26/10
10/30/09
11/27/09
12/25/09

I used this procedure to get the above data but it's not coming out in the oder that I want. I would like 02/26/10 first then 01/29/10 etc... Thanks.

SELECT convert(varchar,DowrDate,1) as DowrDate
FROM TSRPTotals
group by DowrDate
order by DowrDate

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-02-18 : 14:00:21
Assuming your "Dates" are stored as a Date data type:

ORDER BY DateColumn DESC
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-02-18 : 14:22:21
I added this:

SELECT convert(varchar,DowrDate,1) as DowrDate
FROM TSRPTotals
group by DowrDate
order by DowrDate DESC

and got this...

12/25/09
11/27/09
10/30/09
02/26/10
01/29/10

Dowrdate is datetime in the database.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-02-18 : 14:32:12
This works for me...is this your entire query?
declare @t table (col1 datetime)
insert @t
select '20100226'
union all select '20091225'
union all select '20091225'
union all select '20091030'
union all select '20091127'
union all select '20100129'

select convert(varchar,col1,1) from @t
group by col1
order by col1 desc
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-02-18 : 14:57:56
Yes here the entire stored procedure:

ALTER procedure [dbo].[GetMonth]
AS
SELECT convert(varchar,DowrDate,1) as DowrDate
FROM TSRPTotals
group by DowrDate
order by DowrDate DESC
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-02-18 : 14:59:58
It does work but I wanted the 2010 dates listed first so 02/26/2010 should be first. I hope that makes sense.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-02-18 : 15:07:15
Yes..it does order properly ...this is the output..Did you try it?
Dates
------------------------------
02/26/10
01/29/10
12/25/09
11/27/09
10/30/09
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-02-18 : 15:13:22
My mistake yes it works. Sorry about that I was looking at the other query without desc added to it. Thanks!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-19 : 01:24:45
quote:
Originally posted by JJ297

Yes here the entire stored procedure:

ALTER procedure [dbo].[GetMonth]
AS
SELECT convert(varchar,DowrDate,1) as DowrDate
FROM TSRPTotals
group by DowrDate
order by DowrDate DESC


As per your code dates will be ordered by varchars
Thats why you should do formation at front end application if you use it

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-02-19 : 08:50:51
Thanks that was my problem. Once I moved the varchar I got the order I was looking for.
Go to Top of Page
   

- Advertisement -