通过字符串格式化日期字段SQLITE数据库排序查询 [英] SQLITE DB sort query by string formatted date field

查看:1358
本文介绍了通过字符串格式化日期字段SQLITE数据库排序查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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)

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

这篇关于通过字符串格式化日期字段SQLITE数据库排序查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆