February 22, 2014 at 8:22 am
How can I order date in ascending order in which month is in month abbreviation format. here is the data
Date_Month
APR 2012
JAN 2012
AUG 2012
AUG 2013
DEC 2012
I need to order it in this, in which smaller date come first
Date_Month
JAN 2012
APR 2012
AUG 2012
DEC 2012
AUG 2013
this is the date format function in my query select SUBSTRING(CONVERT(VARCHAR(20),GETDATE(),113),4,8)
February 22, 2014 at 1:55 pm
SELECTDate_Month
, LEFT(Date_Month,3) AS Mo3
, CAST(RIGHT(Date_Month,4) AS INT) AS Yr
, CAST(LEFT(Date_Month,3) + ' 1, ' + RIGHT(Date_Month,4) AS DATE) AS DateFun
FROM (
SELECT 'APR 2012' AS Date_Month
UNION ALL
SELECT 'JAN 2012'
UNION ALL
SELECT 'AUG 2012'
UNION ALL
SELECT 'AUG 2013'
UNION ALL
SELECT 'DEC 2012') x
ORDER BY DateFun DESC;
February 23, 2014 at 5:52 pm
Why not just do it like this?
WITH SampleData (Date_Month) AS
(
SELECT 'APR 2012'
UNION ALL SELECT 'JAN 2012'
UNION ALL SELECT 'AUG 2012'
UNION ALL SELECT 'AUG 2013'
UNION ALL SELECT 'DEC 2012'
)
SELECT Date_Month
FROM SampleData
ORDER BY CAST(Date_Month AS DATE);
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
February 24, 2014 at 3:19 pm
February 24, 2014 at 5:20 pm
create a datetime dimension and join to it for the sort
Create Table dimDateTime (
DateOrder int,
MonthDescr varchar(10)
)
insert into dimDateTime values
(1,'JAN 2012'),
(2,'FEB 2012'), etc
select stuff
from MyTable
join dimDateTime
on dateColumn = MonthDescr
order by DateOrder
February 24, 2014 at 5:44 pm
AronB (2/24/2014)
Your solution reminded me of this SQL Spackle article from Jeff Moden.
It could very well be that that is where I remembered this from.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply