Can anyone help with this problem, I’m trying to select the last 12 month of dates from table, then change the format so that the date appears as:
Feb 2013
Jan 2013 etc
I can change the format to above, but that changes the date to string which then makes it impossible to order DESC.
How can I keep the above format, and still order by desc
SELECT DISTINCT TOP 12
--CONVERT(date, NewsDatePosted, 120) AS ArchiveTravelNewsByMonth
--CONVERT(CHAR(4), NewsDatePosted, 100) + CONVERT(CHAR(4), NewsDatePosted, 120) AS MY
--DatePart(Year,NewsDatePosted) AS YearPosted
--datename(month,NewsDatePosted) + ' ' + DatePart(Year,NewsDatePosted) AS YearPosted
FROM dbo.at_News
order by ArchiveTravelNewsByMonth DESC
Thanks
George
SELECT DISTINCT TOP 12 SUBSTRING(CONVERT(VARCHAR(11), NewsDatePosted, 113), 4, 8)
AS ArchiveTravelNewsByMonth,
NewsDatePosted
FROM dbo.at_News
ORDER BY CONVERT(DATETIME, CONVERT(CHAR(4), NewsDatePosted, 100) + CONVERT(CHAR(4), NewsDatePosted, 120))
Please try by adding an order by clause and inner select like