MySQL sort by using a datetime file in ASP.NET

Matt Brown 1 Reputation point
2023-01-05T19:37:04.537+00:00

The completed date is a date type field. If I sort on it, I get the expected results. The create date is a datetime type of field. If I do a sort on it, it doesn't sort correctly and sorts by the request ID. I've done the sort with and without casting it, and I still can't get it to work. I'm merely adding it here to show that I did try to do just that. Various Google searches have also left me with no answers. Any ideas how to make this work?

string cmdString = "SELECT requests.requestid, " +   
                                    "assignee.firstname, " +  
                                    "assignee.lastname, " +  
                                    "astatus.action, " +  
                                    "requests.createDate, " +  
                                    "requests.completionDate, " +  
                                    "requests.hlq, " +  
                                    "hlqdc.name, " +  
                                    "rtype.name as typename, " +  
                                    "module.name as xgroup, " +  
                                    "requestor.firstname as requestorfirstname, " +  
                                    "requestor.lastname as requestorlastname, " +  
                                    "requests.title, " +  
                                    "comments.comment " +  
                                "FROM app.request requests " +  
                                "Left JOIN app.user assignee ON assignee.id = requests.assignedToId " +  
                                "Inner JOIN app.action astatus ON astatus.id = requests.actionid " +  
                                "LEFT JOIN app.datacenter hlqdc ON hlqdc.id = requests.hlqDevCenterId " +  
                                "Inner JOIN app.user requestor ON requestor.id = requests.userid " +  
                                "Left JOIN app.requesttype rtype ON rtype.id = requests.requestTypeId " +  
                                "Inner JOIN app.moduletype module ON module.id = requests.moduleTypeId " +  
                                "Inner JOIN app.comment comments ON comments.requestid = requests.requestid ";  
  
            if (!string.IsNullOrEmpty(sql))  
                cmdString += "Where " + sql;  
              
            cmdString += " Order by CAST(requests.createDate AS DATETIME) limit 1000;";  
Developer technologies | ASP.NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2023-01-05T20:28:53.91+00:00

    Just going out on a limit and asking the question of whether you're sure the ordering is wrong? I'm going to guess that RequestId is an identity column that is sequentially incrementing each time a row is added. Furthermore the CreateDate is probably the timestamp when the row was added. Ignoring rows being added at the same time I would expect the order of the rows to be the same whether you ordered by RequestId or CreateDate. If I saw a RequestId that had a CreateDate higher than the rows after it then I would suspect it was added in a transaction but otherwise wouldn't matter.

    Can you provide us the final version of the query you're running (not the code that is generating it) along with a snapshot of the data you're getting when the ordering seems wrong?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.