Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got a column / class member of type DateTime in my class/SQLite table.

However, both this query:

SQL
SELECT * FROM PhotraxBaseData WHERE dateTimeTaken >= '11/11/2009' AND dateTimeTaken <= '11/11/2014'


-and this one:

SQL
SELECT * FROM PhotraxBaseData WHERE dateTimeTaken BETWEEN '11/11/2009' AND '11/11/2014'


...return no records. Both of these seem to be accepted syntax-wise (they run without error), but return "(0 items)" in LINQPad, even though there are multiple records between those dates.

What query do I need to get the correct subset of data?
Posted
Updated 2-Nov-14 17:59pm
v3

Hi,

Try the following

SELECT * FROM PhotraxBaseData WHERE dateTimeTaken BETWEEN CONVERT(DATETIME,'2009-11-11') AND CONVERT(DATETIME,'2014-11-11');

In your example, you are checking the string value with the datetime variable so it may not gives the correct answer..


While using Datetime Checking in sql , consider that the given date format is in YYYY-mm--dd format -- it avoids the confusion to user and sql server system...

For Sql Lite:

SELECT * FROM PhotraxBaseData WHERE dateTimeTaken BETWEEN '2009-11-11' AND '2014-11-11'

SQL LITE does not support the data type DATETIME .

But in your Query you have to change the date format from 'dd-mm-yyyy' to 'yyyy-mm-dd'
this is the standard date format which is understand by SQL.

Refer the following link regarding SQL LITE Data types..

https://www.sqlite.org/datatype3.html[^]
 
Share this answer
 
v2
Comments
B. Clay Shannon 4-Nov-14 9:27am    
Thanks; a side question: was the ending semicolon deliberate? I've never used that in SQL statements, but I've seen people use it. What is the benefit?
Magesh M N 4-Nov-14 23:22pm    
Refer the following link regarding the using of semicolon
http://sqlmag.com/blog/semicolon
B. Clay Shannon 4-Nov-14 9:32am    
On second thought, this:
SELECT * FROM PhotraxBaseData WHERE dateTimeTaken BETWEEN CONVERT(DATETIME,'2009-11-11') AND CONVERT(DATETIME,'2014-11-11');
...doesn't even run in LINQPad. It gives me, "SQLite error
no such column: DATETIME"
Try Like this.

SQL
SELECT * FROM PhotraxBaseData 
WHERE strftime('%d/%m/%y', dateTimeTaken ) BETWEEN '11/11/2009' AND '11/11/2014'
 
Share this answer
 
Comments
B. Clay Shannon 3-Nov-14 0:55am    
That also returns no records in LINQPad; I'll try it later on the actual SQLite data.
Hi,

I couldn't see any error in the statement, i checked with a sample model and its working fine...

I think it may be sometime the SQLite Datetime settings format is keeping rely with your system/ laptop date format...

Please check the Datetime format or you can convert the '11/11/2014' to Datetime object and then check the same condition...


Thanks,
Ullas Krishnan
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900