1

Joined is a datetime data type column in the database and dates are saved as '12/05/2010 15:54:32' This my query:

SELECT * 
FROM users 
WHERE joined BETWEEN '12/05/2010 00:00:00' AND '12/05/2010 23:59:59' 
ORDER BY id ASC

But it doesn't work. It returns no rows.

So how i gan get them?


Solution:

SELECT * 
FROM users 
WHERE joined BETWEEN '2010-05-05 00:00:00' AND '2010-05-12 23:59:59'
ORDER BY id ASC
1
  • What's the first 59 in 59:59:59 represent? Commented May 13, 2010 at 2:17

2 Answers 2

2

I realize that this is not an answer,
but perhaps it can help in narrowing down where the problem might lie.

Edit:
This also works at the shell:

sqlite> create table t (ts);
sqlite> insert into t values ('12/05/2010 15:54:32');
sqlite> SELECT * 
   ...> FROM t 
   ...> WHERE ts 
   ...> BETWEEN '12/05/2010 00:00:00' AND '12/05/2010 23:59:59';
12/05/2010 15:54:32

The following works for me in Python:

>>> import sqlite3
>>> conn = sqlite3.connect(":memory:")
>>> c = conn.cursor()
>>> c.execute("CREATE TABLE t (ts)")
<sqlite3.Cursor object at 0x7fe88ebbac90>
>>> conn.commit()
>>> c.execute("INSERT INTO t VALUES ('12/05/2010 15:54:32');")
<sqlite3.Cursor object at 0x7fe88ebbac90>
>>> conn.commit()
>>> c.execute("""
SELECT * 
FROM t 
WHERE ts 
BETWEEN '12/05/2010 00:00:00' AND '12/05/2010 23:59:59'
""").fetchall()
[(u'12/05/2010 15:54:32',)]
Sign up to request clarification or add additional context in comments.

5 Comments

Technically is what i'm using. I've tested qhe query in SQLite Administrator without results. The joined field is a datetime and sqlite says that the datetimes should be saved as strings.
And ts is a 'text' or 'datetime' datatype?
@Sein: SQLite doesn't have a 'datetime' datatype. Dates are stored as text.
Here's a reference: sqlite.org/datatype3.html#affinity In this case, I didn't use a type so it defaults to None.
I figured it. To retrieve results i must replace / with - and invert the date: SELECT * FROM users WHERE joined BETWEEN '2010-05-05 00:00:00' AND '2010-05-12 23:59:59' ORDER BY id ASC
1

If you're storing dates as strings, you should use 'YYYY-MM-DD HH:MM:SS' format. These will sort in chronological order, and have the added benefit of being accepted in SQLite's date and time functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.