通过字符串格式化日期字段SQLITE数据库排序查询字段、字符串、日期、数据库

由网友(愁绪入梦遥)分享简介:我有一个SQLite数据库有一个字符串字段,它包含以下格式的日期:01.01.2012。我要的是这个字符串的查询格式的日期来排序。我想这没有成功:I have a SQLITE DB with a string field which contains a date in the following format...

我有一个SQLite数据库有一个字符串字段,它包含以下格式的日期:01.01.2012。 我要的是这个字符串的查询格式的日期来排序。 我想这没有成功:

I have a SQLITE DB with a string field which contains a date in the following format: "01.01.2012". What I want is to sort the by this string formatted date in a query. I tried this without success:

SELECT * FROM fahrer, fahrzeuge, nutzungsarten, fahrtenbuch WHERE fahrtenbuch.nutzungsart_id = nutzungsarten._id AND fahrtenbuch.fahrzeuge_id = fahrzeuge._id AND fahrtenbuch.fahrer_id = fahrer._id ORDER BY strftime ('%d.%m.%Y', fahrtenbuch.startdatum)

我在做什么错了?

What I am doing wrong?

推荐答案

startdatum 列中的值不是在SQLite的识别的格式,所以你不能用它作为参数传递给的strftime 。 (即使它的工作,结果将不能正确地排序,因为日期格式不从最显著领域,这一年。)

The values in the startdatum column are not in a format that SQLite recognizes, so you cannot use it as a parameter to strftime. (Even if it worked, the result would not be sorted correctly because that date format does not begin with the most significant field, the year.)

您可以尝试提取日期字段,这样的排序就相当于使用 YYYY-MM-DD 命令:

You could try to extract the date fields so that the sorting is equivalent with the yyyy-mm-dd order:

SELECT ...
ORDER BY substr(startdatum, 7, 4),
         substr(startdatum, 4, 2),
         substr(startdatum, 1, 2)

不过,我会建议将这些值转换成支持的格式摆在首位。

阅读全文

相关推荐

最新文章