question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ORDER BY and dates issue

See original GitHub issue

Hello, I encounter an issue trying to ordering my data containing date columns.

Here an example of code (Apps script):

function myTest() {
  var data =[{id: 200, fromDate: new Date("2022-11-29"), toDate: new Date("2022-11-29")}, 
    {id:  100, fromDate: new Date("2022-11-30"), toDate: new Date("2022-11-30")},
    {id:  101, fromDate: new Date("2022-11-30"), toDate: new Date("2022-11-30")}, 
    {id:  102, fromDate: new Date("2022-11-30"), toDate: new Date("2022-11-30")}, 
    ];

  var db = new alasql.Database("MyDB");
  db.exec('CREATE TABLE mytable (id INT, fromDate Date, toDate Date)');
  db.tables.mytable.data = data;

  console.log(db.tables.mytable.data);
  var res = db.exec(`SELECT id FROM mytable ORDER BY fromDate, id ASC`,);

  console.log(res);
}

The result I get:

[ {id: 200}, {id: 102}, {id: 101}, {id: 100} ]

But the result I expect is:

[ {id: 200}, {id: 100}, {id: 101}, {id: 102} ]

Any idea of what’s happening ?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Buronicommented, Oct 13, 2022

I’ve spent some time looking into this issue. It seems like there’s several factors involved.

Short answer: You can get round this by setting alasql.options.valueof = true;. For example see this fiddle http://jsfiddle.net/3rbL75hc/

Long answer:

This seems to be partially fixed on the develop branch for 2.1.3. If I recreate @fabads’ example in a local test on develop, it reproduces the error. However if I add fromDate to the select query columns then it works as we’d expect.

# This doesn't work on `develop`
SELECT id FROM mytable ORDER BY fromDate, id ASC

# This does work
SELECT id, fromDate FROM mytable ORDER BY fromDate, id ASC

# Note that neither work on <2.1.3

The issue here is that the date needs to be converted to a Unix epoch via date.valueOf(). This only happens when the column type explicitly is DATE, DATETIME, STRING or NUMBER: see 426orderby.js#L60. However, the fromDate column as well as all associated type information is dropped out of the query object when it’s not explicitly SELECTed.

This is also what causes the issue in @mathiasrw’s example: since it’s an anonymous query, there’s no type information for date so the .valueOf() prefix is never added to the date.

I’m looking into how to get round this, but someone more familiar with the codebase may be able to make a quicker fix with this information!

0reactions
mathiasrwcommented, Dec 27, 2022

Any luck @Buroni ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL ORDER BY date problem - Stack Overflow
It seems that your date column is not of type datetime but varchar. You have to convert it to datetime when sorting: select...
Read more >
Excel date formatting NOT working? Dates not sorting in Excel?
00:00 Intro of the date sort order not working00:36 Reason these ... If you are having date format or sort issues in Excel...
Read more >
Why Don't My Dates Sort Correctly? | Excel Dates Won't Sort
... file here: https://www.bluepecantraining.com/why-dont-my- dates - sort -correctly-excel- dates -wont- sort -oldest-to-newest-or-newest-to-oldes.
Read more >
Sort by dates - Microsoft Support
When dates in a worksheet aren't in the order you want, you can sort them. For example, you can sort them from oldest...
Read more >
Date sorting issue in SQL server. - CodeProject
You are sorting dates as strings, because that is what you have converted them to. Strings always sort in alphabetical order, not chronological....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found