ORDER BY and dates issue
See original GitHub issueHello, 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:
- Created a year ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 for2.1.3
. If I recreate @fabads’ example in a local test ondevelop
, it reproduces the error. However if I addfromDate
to the select query columns then it works as we’d expect.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 isDATE
,DATETIME
,STRING
orNUMBER
: see 426orderby.js#L60. However, thefromDate
column as well as all associated type information is dropped out of the query object when it’s not explicitlySELECT
ed.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!
Any luck @Buroni ?