本文介绍了Javascript:如何对日期字段进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如何使用web sql查询对日期字段进行排序? query ='SELECT * FROM invoice ORDER BY inv_date';原始表格: id inv_date code amt1 1 03/03/2003 C00002 400 2 02/02/2001 C00002 300 3 12/02/2000 C00001 300 排序表: id inv_date code amt1 3 02/02/2001 C00001 300 1 03/03/2003 C00002 400 2 12/02/2000 C00002 300 这是排序后的输出如何从最小的日期到更高的日期正确排序? 任何人都可以租约帮我解决这个问题?解决方案 简单:更改数据库。 停止存储日期(和其他数值)作为字符串:将它们存储为适当的数值数据类型 - 在本例中为DATETIME - 您将能够毫无问题地对它们进行排序和比较。 目前,它们是字符串--NVARCHAR - 所以在任何时候使用的比较,包括排序,都是字符串比较。通过逐个字符查看两个值并将整个比较基于第一个不同的字符对来工作。 可以将日期字符串转换为用于排序的DATETIME,但它依赖于始终正确的值 - 这对于字符串来说不一定正确,因为它很容易在那里得到坏数据。 将数据库排序 - 并始终使用适当的数据类型! 先生,存储值的最佳方法可以说16/07/2014就像数字格式 20140716 一样,在排序时它将自行管理所有内容。 How to sort a date field using web sql query?query = 'SELECT * FROM invoice ORDER BY inv_date';Original table:id inv_date code amt11 03/03/2003 C00002 4002 02/02/2001 C00002 3003 12/02/2000 C00001 300Sorted table:id inv_date code amt13 02/02/2001 C00001 3001 03/03/2003 C00002 4002 12/02/2000 C00002 300This is the output after sorting how can i sort properly from smallest date to higher?Can anybody please help me to trigger this out? 解决方案 Simple: change your database.Stop storing date (and other numeric values) as strings: store them as an appropriate numeric datatype - in this case DATETIME - and you will be able to sort and compare them without problems.At the moment, they are strings - NVARCHAR - so the comparison that is used at all times, including for sorting, is a string comparison. Which works by looking at the two values character by character and bases the whole comparison on the first different pair of characters.It would be possible to convert the date string to a DATETIME for sorting, but that relies on the values always being correct - which is not necessarily correct with strings as it is far too easy to get "bad" data in there.Sort your DB out - and always use appropriate datatypes!sir, best way to store the value lets say "16/07/2014" is like this in numeric format "20140716" and at time of sorting it will manage everything on its own. 这篇关于Javascript:如何对日期字段进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 23:11