简体   繁体   中英

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.

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 is displayed in the datagridview as follow "08/03/2012 11:52 AM". Can you use that format for checking like DateTime(ALARMTIME)?

The only problem have with this SQL Statement is that it always returns zero dataset or records. However, SQLite doesn't complain about the syntax.

Strictly speaking, SQLite doesn't have datetime types for its columns :

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values

The problem here is that the string you're using condition isn't a valid date/time, so it's treated as 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 .)

In addition, make sure that the values in your ALARMTIME are correctly formatted too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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