June 4, 2005 at 4:27 am
Hi guys,
SQL doesnt appear to let you use order by on datename.. eg:
SELECT distinct datename(mm, events.date) as Months
FROM Events
where datepart(month, events.date) between 5 and 6
order by datename(mm, events.date)
is this a known problem?
June 4, 2005 at 6:28 am
Have found a work around.. no worries!
June 5, 2005 at 8:33 am
datename(mm,events.date) will give you 'May' and 'June' from your where clause. Ordering by the NAME will put June first.
Is this what you want? Maybe order by datepart, not datename.
June 5, 2005 at 9:55 am
Fred,
im such a dumbass... of course it's ordering- it's ordering alphabetically! i thought for soem reason it would order still based on JFMAMJASOND order...
der..!
June 5, 2005 at 5:37 pm
Joe's answer is close to correct, but it has a small error in it. He should be using the DATENAME function instead of DATEPART. Also, the DATENAME function returns the entire month name, not the first three characters of the month name. Therefore, the correct solution for the column sort_value is:
select charindex(left(datename(mm,getdate()),3),'JanFebMarAprMayJunJulAugSepOctNovDec') AS sort_value
June 5, 2005 at 5:43 pm
Actually the correct solution to Alex's original question is:
SELECT distinct datename(mm, events.date) as Months
FROM Events
where datepart(month, events.date) between 5 and 6
order by charindex(left(datename(mm,events.date),3),'JanFebMarAprMayJunJulAugSepOctNovDec')
June 6, 2005 at 7:23 am
That set of functions starting with CHARINDEX is clever! I've already cut & pasted it into my box of tricks.
June 6, 2005 at 9:07 am
Just to add even more fuel to this post, it would be better to use the bewteen operator for this task as it would use an index seek and not a scan...
SELECT distinct datename(mm, events.date) as Months
FROM Events
where datepart(month, events.date) between @DateStart and @DateEnd
order by charindex(left(datename(mm,events.date),3),'JanFebMarAprMayJunJulAugSepOctNovDec')
this is assuming that he wants only the events of May and June for the current year.
And wouldn't it be simpler to just do a sort like this ??
Order by datepart(month, events.date)
June 6, 2005 at 9:09 am
Or even...
Order by events.date????????
Because without a second column to sort on, the previous order by doesn't mean a lot...
June 6, 2005 at 11:29 am
--I got an error when I tried. So
/*
Server: Msg 145, Level 15, State 1, Line 47
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
*/
SET NOCOUNT ON
DECLARE @events TABLE
(
[Date] DateTime
)
INSERT @events VALUES ('01/01/2003')
INSERT @events VALUES ('02/01/2003')
INSERT @events VALUES ('03/01/2003')
INSERT @events VALUES ('04/01/2003')
INSERT @events VALUES ('05/01/2003')
INSERT @events VALUES ('06/01/2003')
INSERT @events VALUES ('07/01/2003')
INSERT @events VALUES ('08/01/2003')
INSERT @events VALUES ('09/01/2003')
INSERT @events VALUES ('10/01/2003')
INSERT @events VALUES ('11/01/2003')
INSERT @events VALUES ('12/01/2003')
INSERT @events VALUES ('01/01/2004')
INSERT @events VALUES ('02/01/2004')
INSERT @events VALUES ('03/01/2004')
INSERT @events VALUES ('04/01/2004')
INSERT @events VALUES ('05/01/2004')
INSERT @events VALUES ('06/01/2004')
INSERT @events VALUES ('07/01/2004')
INSERT @events VALUES ('08/01/2004')
INSERT @events VALUES ('09/01/2004')
INSERT @events VALUES ('10/01/2004')
INSERT @events VALUES ('11/01/2004')
INSERT @events VALUES ('12/01/2004')
SELECT DISTINCT datepart(month, A.date) ABR, datename(mm, A.date) as Months
FROM @events A
where datepart(month, A.date) between 1 and 12
ORDER BY datepart(month, A.date)
OR Your's
SELECT DISTINCT charindex(left(datename(mm,A.date),3),'JanFebMarAprMayJunJulAugSepOctNovDec') ABR, datename(mm, A.date) as Months
FROM @Events A
where datepart(month, A.date) between 1 and 12
order by charindex(left(datename(mm,A.date),3),'JanFebMarAprMayJunJulAugSepOctNovDec')
Regards,
gova
June 6, 2005 at 11:57 am
Order by 1 will not be supported in 2005... better get used to put the real column in there.
June 6, 2005 at 2:10 pm
govinn:
Good catch. I didn't unit test the solution first. Without writing a subquery, you need to include the item you want to sort by in the SELECT statement also. Therefore, the two queries you wrote above do execute correctly, except they also output the column's value: either datepart(month,A.date) or
charindex(left(datename(mm,A.date),3),'JanFebMarAprMayJunJulAugSepOctNovDec').
--Jeff
June 6, 2005 at 2:54 pm
i'm sorry - did i miss something?
wouldn't you sort by month like this...
examples using Northwind :
select * from [orders]
order by Month(orderdate)
select * from orders
order by Datepart(mm, orderdate)
June 6, 2005 at 2:57 pm
he wants to sort by Jan, feb, march, apr...
not August, december....
June 21, 2005 at 2:03 pm
exactly...
that's by month alone. here's by month / day - if you need to sort by day as well.
select * from orders
order by Datepart(mm, orderdate), Datepart(dd, orderdate)
http://www.7hertz.com/archives/2004/04/its_your_birthday.html
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply