简体   繁体   English

是否可以在SQL语句中将列从字符串转换为日期时间类型?

[英]Is it possible to convert a column from string to datetime type in a SQL statement?

I need to query SQLite datebase table using the following SQL Statement. 我需要使用以下SQL语句查询SQLite数据库表。

SELECT * 
FROM Alarms 
WHERE ALARMSTATE IN (0,1,2) 
  AND ALARMPRIORITY IN (0,1,2,3,4) 
  AND ALARMGROUP IN (0,1,2,3,4,5,6,7) 
  AND DateTime(ALARMTIME) BETWEEN datetime("2012-08-02 00:00:00") 
                    AND datetime("2012-08-03 00:00:00") 
ORDER BY ALARMTIME DESC

ALARMTIME is of TEXT datatype. ALARMTIME是TEXT数据类型。

ALARMTIME is displayed in the datagridview as follow "08/03/2012 11:52 AM". ALARMTIME显示在datagridview中,如下所示“08/03/2012 11:52 AM”。 Can you use that format for checking like DateTime(ALARMTIME)? 您可以使用该格式进行检查,如DateTime(ALARMTIME)吗?

The only problem have with this SQL Statement is that it always returns zero dataset or records. 这个SQL语句唯一的问题是它总是返回零数据集或记录。 However, SQLite doesn't complain about the syntax. 但是,SQLite并没有抱怨语法。

Strictly speaking, SQLite doesn't have datetime types for its columns : 严格来说,SQLite 的列没有日期时间类型

SQLite does not have a storage class set aside for storing dates and/or times. SQLite没有为存储日期和/或时间而预留的存储类。 Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values 相反,SQLite的内置日期和时间函数能够将日期和时间存储为TEXT,REAL或INTEGER值

The problem here is that the string you're using condition isn't a valid date/time, so it's treated as null: 这里的问题是您使用条件的字符串不是有效的日期/时间,因此它被视为null:

sqlite> SELECT datetime("2012-08-3 00:00:00");

sqlite> SELECT datetime("2012-08-03 00:00:00");
2012-08-03 00:00:00

(Note 2012-08-03 instead of 2012-08-3 .) (注意2012-08-03而不是2012-08-3 。)

In addition, make sure that the values in your ALARMTIME are correctly formatted too. 此外,请确保ALARMTIME中的值ALARMTIME已正确格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM