Order by descending date - month, day and year
I'm guessing EventDate
is a char or varchar and not a date otherwise your order by clause would be fine.
You can use CONVERT to change the values to a date and sort by that
SELECT *
FROM
vw_view
ORDER BY
CONVERT(DateTime, EventDate,101) DESC
The problem with that is, as Sparky points out in the comments, if EventDate has a value that can't be converted to a date the query won't execute.
This means you should either exclude the bad rows or let the bad rows go to the bottom of the results
To exclude the bad rows just add WHERE IsDate(EventDate) = 1
To let let the bad dates go to the bottom you need to use CASE
e.g.
ORDER BY
CASE
WHEN IsDate(EventDate) = 1 THEN CONVERT(DateTime, EventDate,101)
ELSE null
END DESC
orderByDescending date, month and year
You shouldn't to try order the date as string;
var res = objUI.Where(x => x.LastLogin != null)
.GroupBy(x => x.LastLogin.Value.Date)
.OrderByDescending(x => x.Key)
.Select(x => new { LastLogin = string.Format("{0:MM/dd/yyyy}", x.Key) })
.ToList();
Sorting by date & time in descending order?
If you want the last 5 rows, ordered in ascending order, you need a subquery:
SELECT *
FROM
( SELECT id, name, form_id, DATE(updated_at) AS updated_date, updated_at
FROM wp_frm_items
WHERE user_id = 11
AND form_id=9
ORDER BY updated_at DESC
LIMIT 5
) AS tmp
ORDER BY updated_at
After reading the question for 10th time, this may be (just maybe) what you want. Order by Date descending and then order by time (on same date) ascending:
SELECT id, name, form_id, DATE(updated_at) AS updated_date
FROM wp_frm_items
WHERE user_id = 11
AND form_id=9
ORDER BY DATE(updated_at) DESC
, updated_at ASC
LINQ orderby on date field in descending order
I don't believe that Distinct() is guaranteed to maintain the order of the set.
Try pulling out an anonymous type first and distinct/sort on that before you convert to string:
var ud = env.Select(d => new
{
d.ReportDate.Year,
d.ReportDate.Month,
FormattedDate = d.ReportDate.ToString("yyyy-MMM")
})
.Distinct()
.OrderByDescending(d => d.Year)
.ThenByDescending(d => d.Month)
.Select(d => d.FormattedDate);
Sorting year/month string as descending dates
You just need to get the strings in a format that is easily comparable, either as strings or dates. However, using Dates gives an added issue of how to ensure 1933 is correctly sorted between 1932/12 and 1933/1.
The following sorts as strings by appending "/00" to any value that is only a year, and adding a leading zero to single digit months so "2015/3" is compared as "2015/03".
var values = [{value: "2015"}, {value:"2015/3"}, {value: "2015/10"}, {value: "1933"}, {value: "1932/12"}, {value:"1933/11"}, {value: "1933/1"}];
console.log(JSON.stringify(values));
values.sort(function(a, b) {
function fixMonth(date) {
var t = date.split('/');
return t[0] + '/' + ('0'+t[1]).slice(-2);
}
a = /\//.test(a.value)? fixMonth(a.value) : a.value + '/00';
b = /\//.test(b.value)? fixMonth(b.value) : b.value + '/00';
return a.localeCompare(b);
})
console.log(JSON.stringify(values));
Orderby month and year using linq
You can use DateTime.TryParseExact
like this:
var months = ((from mnths in context.Table
orderby mnths.Month
select mnths.Month).Distinct()).ToList().OrderBy(m =>
{
DateTime month;
return DateTime.TryParseExact(m, new[] {"MMM-yy"}, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out month)
? month
: DateTime.MinValue;
}).ToList();
You need to call ToList
before OrderBy
so the original query is executed and you get an IEnumerable<string>
instead of an IQueryable
.
JavaScript Ordering List of Dates in Descending Order
In your sort
callback, return the difference of the dates expressed in epochs (number of milliseconds). You can use Date.parse
for this:
return Date.parse(date1) - Date.parse(date2);
let dates = ['Oct 7, 2009', 'Nov 10, 2009', 'Jan 10, 2009', 'Oct 22, 2009'];
let sortDate = function (date1, date2) {
return Date.parse(date1) - Date.parse(date2);
}
dates.sort(sortDate)
for (let i = 0; i < dates.length; i++) {
console.log(i + ': ' + dates[i])
}
Related Topics
Query Several Nextval from Sequence in One Statement
Pivot Without Aggregate Function in Mssql 2008 R2
Oracle - Update Join - Non Key-Preserved Table
Google Big Query SQL - Get Most Recent Column Value
Update Multiple Columns in a Trigger Function in Plpgsql
Querying Active Directory from SQL Server 2005
How to Detect If a String Contains Special Characters
Curious Issue with Oracle Union and Order By
In VS or of Oracle, Which Faster
What Is the Problem with Foreign Key Cascade Multiple Paths and Cycles
How to Create Daylight Savings Time Start and End Function in SQL Server
Return a Query from a Function